From af38d56c0dabb4c8261e4f658271e3eefd435112 Mon Sep 17 00:00:00 2001 From: olayiwola-compucorp Date: Thu, 30 Sep 2021 13:04:41 +0100 Subject: [PATCH 1/2] Add hooks to alter Mosaico plugins --- CRM/Mosaico/Page/Editor.php | 23 +++++++++++++++++++++ templates/CRM/Mosaico/Page/Editor.tpl | 2 +- templates/CRM/Mosaico/Page/EditorIframe.tpl | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CRM/Mosaico/Page/Editor.php b/CRM/Mosaico/Page/Editor.php index ec45212922..ec89045f2c 100644 --- a/CRM/Mosaico/Page/Editor.php +++ b/CRM/Mosaico/Page/Editor.php @@ -14,6 +14,7 @@ public function run() { $this->createMosaicoConfig(), defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0 )); + $smarty->assign('mosaicoPlugins', $this->getMosaicoPlugins()); echo $smarty->fetch(self::getTemplateFileName()); CRM_Utils_System::civiExit(); } @@ -152,4 +153,26 @@ protected function getMaxFileSize() { return (int) min($iniVal, $settingVal); } + /** + * Gets the plugins for `Mosaico.init()`. + * + * @return array + */ + public function getMosaicoPlugins() { + $plugins = []; + + // Allow plugins to be added by a hook. + if (class_exists('\Civi\Core\Event\GenericHookEvent')) { + \Civi::dispatcher()->dispatch('hook_civicrm_mosaicoPlugin', + \Civi\Core\Event\GenericHookEvent::create([ + 'plugins' => &$plugins, + ]) + ); + } + + $plugins = '[ ' . implode(',', $plugins) . ' ]'; + + return $plugins; + } + } diff --git a/templates/CRM/Mosaico/Page/Editor.tpl b/templates/CRM/Mosaico/Page/Editor.tpl index 0b03b55fa5..edd91a8322 100644 --- a/templates/CRM/Mosaico/Page/Editor.tpl +++ b/templates/CRM/Mosaico/Page/Editor.tpl @@ -23,7 +23,7 @@ return; } - var plugins; + var plugins = {/literal}{$mosaicoPlugins}{literal}; // A basic plugin that expose the "viewModel" object as a global variable. // plugins = [function(vm) {window.viewModel = vm;}]; var config = {/literal}{$mosaicoConfig}{literal}; diff --git a/templates/CRM/Mosaico/Page/EditorIframe.tpl b/templates/CRM/Mosaico/Page/EditorIframe.tpl index 6e8fd6c758..c2bbd74385 100644 --- a/templates/CRM/Mosaico/Page/EditorIframe.tpl +++ b/templates/CRM/Mosaico/Page/EditorIframe.tpl @@ -22,7 +22,7 @@ return; } - var plugins = []; + var plugins = {/literal}{$mosaicoPlugins}{literal}; var config = {/literal}{$mosaicoConfig}{literal}; window.addEventListener('beforeunload', function(e) { From a2cf0bbc383187cf87bfe60be417ac8bce2c42db Mon Sep 17 00:00:00 2001 From: olayiwola-compucorp Date: Fri, 19 Nov 2021 11:51:18 +0100 Subject: [PATCH 2/2] Add docs for the hooks that alter mosaico plugins --- docs/api.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index c026dc6e4a..7fc395e9db 100644 --- a/docs/api.md +++ b/docs/api.md @@ -102,4 +102,35 @@ function campaignadv_civicrm_mosaicoScriptUrlsAlter(&$scriptUrls) { $url = CRM_Utils_System::url('civicrm/campaignadv/mosaico-js', '', TRUE, NULL, NULL, NULL, NULL); $scriptUrls[] = $url; } -``` \ No newline at end of file +``` + +## hook_civicrm_mosaicoPlugin(&$plugins) + +This hook can be implemented to add custom JS plugins to the Mosaico object during initialization, see [here](https://github.com/voidlabs/mosaico/wiki/Mosaico-Plugins) on how plugins work in Mosaico. + +Example usage: +``` +/** + * Implements hook_civicrm_mosaicoPlugin(). + */ +function example_civicrm_mosaicoPlugin(&$plugins) { + $plugins[] = _example_alert_plugin(); +} + +/** + * Example plugin to display alert on init and dispose. + */ +function _example_alert_plugin(){ + $plugin = <<< JS + function(vm) { + alert("test-plugin"); + return { + init: function(vm){alert("init");}, + dispose: function(vm){alert("dispose");} + } + } + JS; + + return $plugin; +} +```