From 156fd9b9cc7cb9fc9c719c280484481017f21373 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 24 Oct 2014 21:37:30 -0400 Subject: [PATCH] CRM-15524 - Support addSetting in snippet mode --- CRM/Core/Resources.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 191796a3d4..38680eef6f 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -226,8 +226,9 @@ class CRM_Core_Resources { public function addSetting($settings) { $this->settings = $this->mergeSettings($this->settings, $settings); if (!$this->addedSettings) { + $region = self::isAjaxMode() ? 'page-body' : 'html-header'; $resources = $this; - CRM_Core_Region::instance('html-header')->add(array( + CRM_Core_Region::instance($region)->add(array( 'callback' => function(&$snippet, &$html) use ($resources) { $html .= "\n" . $resources->renderSetting(); }, @@ -284,7 +285,14 @@ class CRM_Core_Resources { * @return string */ public function renderSetting() { - $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; + // On a standard page request we construct the CRM object from scratch + if (!self::isAjaxMode()) { + $js = 'var CRM = ' . json_encode($this->getSettings()) . ';'; + } + // For an ajax request we append to it + else { + $js = 'CRM.$.extend(true, CRM, ' . json_encode($this->getSettings()) . ');'; + } return sprintf("\n", $js); } @@ -458,7 +466,7 @@ class CRM_Core_Resources { * @access public */ public function addCoreResources($region = 'html-header') { - if (!isset($this->addedCoreResources[$region])) { + if (!isset($this->addedCoreResources[$region]) && !self::isAjaxMode()) { $this->addedCoreResources[$region] = TRUE; $config = CRM_Core_Config::singleton(); @@ -658,4 +666,11 @@ class CRM_Core_Resources { return $items; } + + /** + * @return bool - is this page request an ajax snippet? + */ + static function isAjaxMode() { + return in_array(CRM_Utils_Array::value('snippet', $_REQUEST), array(CRM_Core_Smarty::PRINT_SNIPPET, CRM_Core_Smarty::PRINT_NOFORM, CRM_Core_Smarty::PRINT_JSON)); + } } -- 2.25.1