From 007b7d354d82d5e0267a76904850543f8a905bc4 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 5 Aug 2020 04:11:25 -0700 Subject: [PATCH] CollectionTrait - Support addScript(), addScriptUrl(), addStyle(), addStyleUrl() --- CRM/Core/Resources.php | 32 ++--------- CRM/Core/Resources/CollectionTrait.php | 75 +++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 29 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 0d086f40c9..a0d9d0ca99 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -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; } diff --git a/CRM/Core/Resources/CollectionTrait.php b/CRM/Core/Resources/CollectionTrait.php index 8b09a1cb40..cdf07da9c6 100644 --- a/CRM/Core/Resources/CollectionTrait.php +++ b/CRM/Core/Resources/CollectionTrait.php @@ -13,6 +13,10 @@ * 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