CollectionTrait - Support addScript(), addScriptUrl(), addStyle(), addStyleUrl()
authorTim Otten <totten@civicrm.org>
Wed, 5 Aug 2020 11:11:25 +0000 (04:11 -0700)
committerSeamus Lee <seamuslee001@gmail.com>
Thu, 3 Sep 2020 22:01:59 +0000 (08:01 +1000)
CRM/Core/Resources.php
CRM/Core/Resources/CollectionTrait.php

index 0d086f40c908842d0f77c8bc4a7b56fc05ee13d4..a0d9d0ca991e46b9752bc1b382e7d109180458de 100644 (file)
@@ -203,13 +203,7 @@ class CRM_Core_Resources {
    * @return CRM_Core_Resources
    */
   public function addScriptUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
-    CRM_Core_Region::instance($region)->add([
-      'name' => $url,
-      'type' => 'scriptUrl',
-      'scriptUrl' => $url,
-      'weight' => $weight,
-      'region' => $region,
-    ]);
+    CRM_Core_Region::instance($region)->add(['scriptUrl' => $url, 'weight' => $weight]);
     return $this;
   }
 
@@ -225,13 +219,7 @@ class CRM_Core_Resources {
    * @return CRM_Core_Resources
    */
   public function addScript($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
-    CRM_Core_Region::instance($region)->add([
-        // 'name' => automatic
-      'type' => 'script',
-      'script' => $code,
-      'weight' => $weight,
-      'region' => $region,
-    ]);
+    CRM_Core_Region::instance($region)->add(['script' => $code, 'weight' => $weight]);
     return $this;
   }
 
@@ -430,13 +418,7 @@ class CRM_Core_Resources {
    * @return CRM_Core_Resources
    */
   public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
-    CRM_Core_Region::instance($region)->add([
-      'name' => $url,
-      'type' => 'styleUrl',
-      'styleUrl' => $url,
-      'weight' => $weight,
-      'region' => $region,
-    ]);
+    CRM_Core_Region::instance($region)->add(['styleUrl' => $url, 'weight' => $weight]);
     return $this;
   }
 
@@ -452,13 +434,7 @@ class CRM_Core_Resources {
    * @return CRM_Core_Resources
    */
   public function addStyle($code, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
-    CRM_Core_Region::instance($region)->add([
-        // 'name' => automatic
-      'type' => 'style',
-      'style' => $code,
-      'weight' => $weight,
-      'region' => $region,
-    ]);
+    CRM_Core_Region::instance($region)->add(['style' => $code, 'weight' => $weight]);
     return $this;
   }
 
index 8b09a1cb4090113b6940fbca7c5d482e36a38674..cdf07da9c6765ad34b20cf6ee89e67abe8449d48 100644 (file)
  * Class CRM_Core_Resources_CollectionTrait
  *
  * This is a building-block for creating classes which maintain a list of resources.
+ *
+ * The class is generally organized in two sections: First, we have core
+ * bit that manages a list of '$snippets'. Second, we have a set of helper
+ * functions which add some syntactic sugar for the snippets.
  */
 trait CRM_Core_Resources_CollectionTrait {
 
@@ -102,7 +106,16 @@ trait CRM_Core_Resources_CollectionTrait {
       throw new \RuntimeException("Unsupported snippet type: " . $snippet['type']);
     }
     if (!isset($snippet['name'])) {
-      $snippet['name'] = count($this->snippets);
+      switch ($snippet['type']) {
+        case 'scriptUrl':
+        case 'styleUrl':
+          $snippet['name'] = $snippet[$snippet['type']];
+          break;
+
+        default:
+          $snippet['name'] = count($this->snippets);
+          break;
+      }
     }
 
     $this->snippets[$snippet['name']] = $snippet;
@@ -237,4 +250,64 @@ trait CRM_Core_Resources_CollectionTrait {
     return 0;
   }
 
+  // -----------------------------------------------
+
+  /**
+   * Add a JavaScript file to the current page using <SCRIPT SRC>.
+   *
+   * @param string $code
+   *   JavaScript source code.
+   * @param array $options
+   *   Open-ended list of options (per add())
+   *   Ex: ['weight' => 123]
+   * @return static
+   */
+  public function addScript(string $code, array $options = []) {
+    $this->add($options + ['script' => $code]);
+    return $this;
+  }
+
+  /**
+   * Add a JavaScript file to the current page using <SCRIPT SRC>.
+   *
+   * @param string $url
+   * @param array $options
+   *   Open-ended list of options (per add())
+   *   Ex: ['weight' => 123]
+   * @return static
+   */
+  public function addScriptUrl(string $url, array $options = []) {
+    $this->add($options + ['scriptUrl' => $url]);
+    return $this;
+  }
+
+  /**
+   * Add a CSS content to the current page using <STYLE>.
+   *
+   * @param string $code
+   *   CSS source code.
+   * @param array $options
+   *   Open-ended list of options (per add())
+   *   Ex: ['weight' => 123]
+   * @return static
+   */
+  public function addStyle(string $code, array $options = []) {
+    $this->add($options + ['style' => $code]);
+    return $this;
+  }
+
+  /**
+   * Add a CSS file to the current page using <LINK HREF>.
+   *
+   * @param string $url
+   * @param array $options
+   *   Open-ended list of options (per add())
+   *   Ex: ['weight' => 123]
+   * @return static
+   */
+  public function addStyleUrl(string $url, array $options = []) {
+    $this->add($options + ['styleUrl' => $url]);
+    return $this;
+  }
+
 }