From 2b2878a9b6240a115aaa55f0a525d05fccde5373 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Tue, 24 Mar 2020 12:33:10 +0000 Subject: [PATCH] Allow adding variables to CRM.vars in any region --- CRM/Core/Resources.php | 50 ++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 99448dd0da..b4532ff26c 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -53,7 +53,6 @@ class CRM_Core_Resources { * @var array */ protected $settings = []; - protected $addedSettings = FALSE; /** * Setting factories. @@ -80,6 +79,15 @@ class CRM_Core_Resources { */ protected $addedCoreStyles = []; + /** + * Added settings. + * + * Format is ($regionName => bool). + * + * @var array + */ + protected $addedSettings = []; + /** * A value to append to JS/CSS URLs to coerce cache resets. * @@ -257,12 +265,15 @@ class CRM_Core_Resources { * @param string $nameSpace * Usually the name of your extension. * @param array $vars + * @param string $region + * The region to add settings to (eg. for payment processors usually billing-block) + * * @return CRM_Core_Resources */ - public function addVars($nameSpace, $vars) { + public function addVars($nameSpace, $vars, $region = NULL) { $existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $this->settings), []); $vars = $this->mergeSettings($existing, $vars); - $this->addSetting(['vars' => [$nameSpace => $vars]]); + $this->addSetting(['vars' => [$nameSpace => $vars]], $region); return $this; } @@ -272,21 +283,28 @@ class CRM_Core_Resources { * Extensions and components should generally use addVars instead. * * @param array $settings + * @param string $region + * The region to add settings to (eg. for payment processors usually billing-block) + * * @return CRM_Core_Resources */ - public function addSetting($settings) { - $this->settings = $this->mergeSettings($this->settings, $settings); - if (!$this->addedSettings) { + public function addSetting($settings, $region = NULL) { + if (!$region) { $region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header'; - $resources = $this; - CRM_Core_Region::instance($region)->add([ - 'callback' => function (&$snippet, &$html) use ($resources) { - $html .= "\n" . $resources->renderSetting(); - }, - 'weight' => -100000, - ]); - $this->addedSettings = TRUE; } + $this->settings = $this->mergeSettings($this->settings, $settings); + if (isset($this->addedSettings[$region])) { + return $this; + } + $resources = $this; + $settingsResource = [ + 'callback' => function (&$snippet, &$html) use ($resources, $region) { + $html .= "\n" . $resources->renderSetting($region); + }, + 'weight' => -100000, + ]; + CRM_Core_Region::instance($region)->add($settingsResource); + $this->addedSettings[$region] = TRUE; return $this; } @@ -337,9 +355,9 @@ class CRM_Core_Resources { * * @return string */ - public function renderSetting() { + public function renderSetting($region = NULL) { // On a standard page request we construct the CRM object from scratch - if (!self::isAjaxMode()) { + if (($region === 'html-header') || !self::isAjaxMode()) { $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; } // For an ajax request we append to it -- 2.25.1