/**
* Maintain a set of markup/templates to inject inside various regions
*/
-class CRM_Core_Region {
+class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Core_Resources_CollectionAdderInterface {
/**
* Obtain the content for a given region.
return Civi::$statics[__CLASS__][$name];
}
- use CRM_Core_Resources_CollectionTrait {
- CRM_Core_Resources_CollectionTrait::add as _add;
- }
+ use CRM_Core_Resources_CollectionTrait;
/**
* Symbolic name of this region
*/
public function __construct($name) {
$this->_name = $name;
- $this->types = ['markup', 'template', 'callback', 'scriptUrl', 'script', 'jquery', 'settings', 'style', 'styleUrl'];
+ $this->types = ['markup', 'template', 'callback', 'scriptFile', 'scriptUrl', 'script', 'jquery', 'settings', 'style', 'styleFile', 'styleUrl'];
$this->defaults['region'] = $name;
// Placeholder which represents any of the default content generated by the main Smarty template
if (is_array($this->snippets['default'])) {
$this->snippets['default']['markup'] = $default;
}
- // We hand as much of the work off to the CMS as possible
- $cms = CRM_Core_Config::singleton()->userSystem;
+
+ Civi::dispatcher()->dispatch('civi.region.render', \Civi\Core\Event\GenericHookEvent::create(['region' => $this]));
$this->sort();
+ $cms = CRM_Core_Config::singleton()->userSystem;
$smarty = CRM_Core_Smarty::singleton();
$html = '';
- foreach ($this->snippets as $snippet) {
- if ($snippet['disabled']) {
- continue;
- }
+
+ $renderSnippet = function($snippet) use (&$html, $smarty, $cms, $allowCmsOverride, &$renderSnippet) {
switch ($snippet['type']) {
case 'markup':
$html .= $snippet['markup'];
break;
case 'jquery':
- $snippet['script'] = sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']);
- // no break - continue processing as script
+ $renderSnippet([
+ 'type' => 'script',
+ 'script' => sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']),
+ ]);
+ break;
+
+ case 'scriptFile':
+ foreach ($snippet['scriptFileUrls'] as $url) {
+ $html .= $renderSnippet(['type' => 'scriptUrl', 'scriptUrl' => $url] + $snippet);
+ }
+ break;
+
case 'script':
if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) {
$html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $snippet['script']);
}
break;
+ case 'styleFile':
+ foreach ($snippet['styleFileUrls'] as $url) {
+ $html .= $renderSnippet(['type' => 'styleUrl', 'styleUrl' => $url] + $snippet);
+ }
+ break;
+
case 'styleUrl':
if (!$allowCmsOverride || !$cms->addStyleUrl($snippet['styleUrl'], $this->_name)) {
$html .= sprintf("<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\"/>\n", $snippet['styleUrl']);
break;
case 'settings':
- $settingsData = json_encode(Civi::resources()->getSettings($this->_name), JSON_UNESCAPED_SLASHES);
+ $settingsData = json_encode($this->getSettings(), JSON_UNESCAPED_SLASHES);
$js = "(function(vars) {
if (window.CRM) CRM.$.extend(true, CRM, vars); else window.CRM = vars;
})($settingsData)";
throw new CRM_Core_Exception(ts('Snippet type %1 is unrecognized',
[1 => $snippet['type']]));
}
+ };
+
+ foreach ($this->snippets as $snippet) {
+ if (empty($snippet['disabled'])) {
+ $renderSnippet($snippet);
+ }
}
return $html;
}