Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks to alter Mosaico plugins #470

Merged
merged 2 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CRM/Mosaico/Page/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

}
33 changes: 32 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
```

## 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;
}
```
2 changes: 1 addition & 1 deletion templates/CRM/Mosaico/Page/Editor.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Mosaico/Page/EditorIframe.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
return;
}

var plugins = [];
var plugins = {/literal}{$mosaicoPlugins}{literal};
var config = {/literal}{$mosaicoConfig}{literal};

window.addEventListener('beforeunload', function(e) {
Expand Down