Skip to content

Commit

Permalink
Merge pull request #16888 from mattwire/addvarsanyregion
Browse files Browse the repository at this point in the history
Allow adding variables to CRM.vars in any region
  • Loading branch information
colemanw authored Apr 9, 2020
2 parents 98317ed + 2b2878a commit cee5dde
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions CRM/Core/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class CRM_Core_Resources {
* @var array
*/
protected $settings = [];
protected $addedSettings = FALSE;

/**
* Setting factories.
Expand All @@ -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.
*
Expand Down Expand Up @@ -257,12 +265,15 @@ public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self:
* @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;
}

Expand All @@ -272,21 +283,28 @@ public function addVars($nameSpace, $vars) {
* 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;
}

Expand Down Expand Up @@ -337,9 +355,9 @@ protected function mergeSettings($settings, $additions) {
*
* @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
Expand Down

0 comments on commit cee5dde

Please sign in to comment.