From b41dbde6bfe916bc4084557ec9163ea446dcccfd Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 5 Aug 2020 05:44:00 -0700 Subject: [PATCH] (REF) CRM_Core_Region - Extract $renderSnippet() This makes it possible for renderSnippet to work recursively. For example, the current `jquery` type can build on the `script` type, and the hypothetical `scriptFile` type could build on the `scriptUrl` type. --- CRM/Core/Region.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/CRM/Core/Region.php b/CRM/Core/Region.php index 0d6cfc6330..6c5da95e10 100644 --- a/CRM/Core/Region.php +++ b/CRM/Core/Region.php @@ -62,17 +62,14 @@ class CRM_Core_Region { 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; $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']; @@ -97,8 +94,12 @@ class CRM_Core_Region { 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 'script': if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) { $html .= sprintf("\n", $snippet['script']); @@ -129,6 +130,12 @@ class CRM_Core_Region { 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; } -- 2.25.1