Skip to content
Snippets Groups Projects
Commit e705013f authored by mattwire's avatar mattwire
Browse files

Add compat functions to work around issues with addVars

parent ce6554d8
Branches
Tags
1 merge request!60.7
<?php
/**
* https://civicrm.org/licensing
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use CRM_Mjwshared_ExtensionUtil as E;
......
<?php
/**
* Class CRM_Mjwshared_Resources
*
* Currently (CiviCRM 5.24) \Civi::resources()->addVars() / CRM_Core_Resources::singleton()->addVars()
* only allows adding to ajax-snippet or html-header region. But we need to add to billing-block region
* @todo See https://github.com/civicrm/civicrm-core/pull/16888 for a core fix which will allow us to
* remove these compatibility functions
*/
class CRM_Mjwshared_Resources extends CRM_Core_Resources {
/**
* Added settings.
*
* Format is ($regionName => bool).
*
* @var array
*/
protected $addedSettingsToRegion = [];
/**
* Add JavaScript variables to CRM.vars
*
* Example:
* From the server:
* CRM_Core_Resources::singleton()->addVars('myNamespace', array('foo' => 'bar'));
* Access var from javascript:
* CRM.vars.myNamespace.foo // "bar"
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC/Javascript+Reference
*
* @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 static function compatAddVars($nameSpace, $vars, $region = NULL) {
$resourcesObject = CRM_Core_Resources::singleton();
$existing = CRM_Utils_Array::value($nameSpace, CRM_Utils_Array::value('vars', $resourcesObject->settings), []);
$vars = $resourcesObject->mergeSettings($existing, $vars);
self::compatAddSetting($resourcesObject, ['vars' => [$nameSpace => $vars]], $region);
return $resourcesObject;
}
/**
* Add JavaScript variables to the root of the CRM object.
* This function is usually reserved for low-level system use.
* Extensions and components should generally use addVars instead.
*
* @param \CRM_Core_Resources $resourcesObject
* @param array $settings
* @param string $region
* The region to add settings to (eg. for payment processors usually billing-block
*
* @return CRM_Core_Resources
*/
public static function compatAddSetting($resourcesObject, $settings, $region = NULL) {
if (!$region) {
$region = self::isAjaxMode() ? 'ajax-snippet' : 'html-header';
}
$resourcesObject->settings = $resourcesObject->mergeSettings($resourcesObject->settings, $settings);
if (isset($resourcesObject->addedSettingsToRegion[$region])) {
return $resourcesObject;
}
$resources = $resourcesObject;
$settingsResource = [
'callback' => function (&$snippet, &$html) use ($resources, $region) {
$html .= "\n" . self::compatRenderSetting($resources, $region);
},
'weight' => -100000,
];
CRM_Core_Region::instance($region)->add($settingsResource);
$resourcesObject->addedSettingsToRegion[$region] = TRUE;
return $resourcesObject;
}
/**
* Helper fn for addSetting.
* Render JavaScript variables for the global CRM object.
*
* @param \CRM_Core_Resources $resourcesObject
*
* @return string
*/
public static function compatRenderSetting($resourcesObject, $region) {
// On a standard page request we construct the CRM object from scratch
if ($region === 'html-header') {
$js = 'var CRM = ' . json_encode($resourcesObject->getSettings()) . ';';
}
// For an ajax request we append to it
else {
$js = 'CRM.$.extend(true, CRM, ' . json_encode($resourcesObject->getSettings()) . ');';
}
return sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment