Move l10n.js to coreResourcesList
authorColeman Watts <coleman@civicrm.org>
Sat, 16 Feb 2019 20:13:01 +0000 (15:13 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 16 Feb 2019 20:13:01 +0000 (15:13 -0500)
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
tests/phpunit/CRM/Core/ResourcesTest.php

index 21401e24e6a93fad0499ece7cf64dc669bb1785f..412731d34a8b9dfd21957fcfcf7b51527dcbf7ec 100644 (file)
@@ -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) === '/');
+  }
+
 }
index 99890e95410f86fdd98264e148deaecd3adf3198..8feda007962370da7d100a545cb553b8aaf0e829 100644 (file)
@@ -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));
+  }
+
 }