From adcd4bf79b8f0343249d40c39c09322217322ff6 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 16 Feb 2019 15:13:01 -0500 Subject: [PATCH] Move l10n.js to coreResourcesList This change allows core resources to be added to the list AFTER l10n variables have been initialized. This is needed by crm.menubar.js. Another upshot of this change is that extensions will be able to add items to the list as fully-formed urls. --- CRM/Core/Resources.php | 38 ++++++++++++++++-------- tests/phpunit/CRM/Core/ResourcesTest.php | 24 +++++++++++++++ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 21401e24e6..412731d34a 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -591,24 +591,18 @@ class CRM_Core_Resources { if (is_array($item)) { $this->addSetting($item); } - elseif (substr($item, -2) == 'js') { + elseif (strpos($item, '.css')) { + $this->isFullyFormedUrl($item) ? $this->addStyleUrl($item, -100, $region) : $this->addStyleFile('civicrm', $item, -100, $region); + } + elseif ($this->isFullyFormedUrl($item)) { + $this->addScriptUrl($item, $jsWeight++, $region); + } + else { // Don't bother looking for ts() calls in packages, there aren't any $translate = (substr($item, 0, 3) == 'js/'); $this->addScriptFile('civicrm', $item, $jsWeight++, $region, $translate); } - else { - $this->addStyleFile('civicrm', $item, -100, $region); - } } - - $tsLocale = CRM_Core_I18n::getLocale(); - // Dynamic localization script - $args = [ - 'r' => $this->getCacheCode(), - 'cid' => CRM_Core_Session::getLoggedInContactID(), - ]; - $this->addScriptUrl(CRM_Utils_System::url('civicrm/ajax/l10n-js/' . $tsLocale, $args, FALSE, NULL, FALSE), $jsWeight++, $region); - // Add global settings $settings = array( 'config' => array( @@ -733,6 +727,13 @@ class CRM_Core_Resources { "js/crm.ajax.js", "js/wysiwyg/crm.wysiwyg.js", ); + + // Dynamic localization script + $items[] = $this->addCacheCode( + CRM_Utils_System::url('civicrm/ajax/l10n-js/' . CRM_Core_I18n::getLocale(), + ['cid' => CRM_Core_Session::getLoggedInContactID()], FALSE, NULL, FALSE) + ); + // add wysiwyg editor $editor = Civi::settings()->get('editor_id'); if ($editor == "CKEditor") { @@ -940,4 +941,15 @@ class CRM_Core_Resources { return $url . $operator . 'r=' . $this->cacheCode; } + /** + * Checks if the given URL is fully-formed + * + * @param string $url + * + * @return bool + */ + public static function isFullyFormedUrl($url) { + return (substr($url, 0, 4) === 'http') || (substr($url, 0, 1) === '/'); + } + } diff --git a/tests/phpunit/CRM/Core/ResourcesTest.php b/tests/phpunit/CRM/Core/ResourcesTest.php index 99890e9541..8feda00796 100644 --- a/tests/phpunit/CRM/Core/ResourcesTest.php +++ b/tests/phpunit/CRM/Core/ResourcesTest.php @@ -397,4 +397,28 @@ class CRM_Core_ResourcesTest extends CiviUnitTestCase { ); } + /** + * return array + */ + public function urlsToCheckIfFullyFormed() { + return [ + ['civicrm/test/page', FALSE], + ['#', FALSE], + ['', FALSE], + ['/civicrm/test/page', TRUE], + ['http://test.com/civicrm/test/page', TRUE], + ['https://test.com/civicrm/test/page', TRUE], + ]; + } + + /** + * @param string $url + * @param string $expected + * + * @dataProvider urlsToCheckIfFullyFormed + */ + public function testIsFullyFormedUrl($url, $expected) { + $this->assertEquals($expected, CRM_Core_Resources::isFullyFormedUrl($url)); + } + } -- 2.25.1