From fce22e157147a8337b44a0aa7572b59cb99a6fba Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 5 Aug 2020 06:57:14 -0700 Subject: [PATCH] CollectionTrait - Support addScriptFile(), addStyleFile() --- CRM/Core/Resources.php | 41 ++++++++++++------ CRM/Core/Resources/CollectionTrait.php | 59 ++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index 0871810ccc..9b694bb03a 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -178,12 +178,14 @@ class CRM_Core_Resources { * @throws \CRM_Core_Exception */ public function addScriptFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION, $translate = TRUE) { - if ($translate) { - $domain = ($translate === TRUE) ? $ext : $translate; - $this->addString($this->strings->get($domain, $this->getPath($ext, $file), 'text/javascript'), $domain); - } - $url = $this->getUrl($ext, $this->filterMinify($ext, $file), TRUE); - return $this->addScriptUrl($url, $weight, $region); + CRM_Core_Region::instance($region)->addScriptFile($ext, $file, [ + 'weight' => $weight, + 'translate' => $translate, + 'name' => "$ext:$file", + // Setting the name above may appear superfluous, but it preserves a historical quirk + // where Region::add() and Resources::addScriptFile() produce slightly different orderings.. + ]); + return $this; } /** @@ -197,7 +199,13 @@ 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(['scriptUrl' => $url, 'weight' => $weight]); + CRM_Core_Region::instance($region)->add([ + 'scriptUrl' => $url, + 'weight' => $weight, + 'name' => $url, + // Setting the name above may appear superfluous, but it preserves a historical quirk + // where Region::add() and Resources::addScriptUrl() produce slightly different orderings.. + ]); return $this; } @@ -324,11 +332,12 @@ class CRM_Core_Resources { * @return CRM_Core_Resources */ public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) { - /** @var Civi\Core\Themes $theme */ - $theme = Civi::service('themes'); - foreach ($theme->resolveUrls($theme->getActiveThemeKey(), $ext, $file) as $url) { - $this->addStyleUrl($url, $weight, $region); - } + CRM_Core_Region::instance($region)->addStyleFile($ext, $file, [ + 'weight' => $weight, + 'name' => "$ext:$file", + // Setting the name above may appear superfluous, but it preserves a historical quirk + // where Region::add() and Resources::addScriptUrl() produce slightly different orderings.. + ]); return $this; } @@ -343,7 +352,13 @@ 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(['styleUrl' => $url, 'weight' => $weight]); + CRM_Core_Region::instance($region)->add([ + 'styleUrl' => $url, + 'weight' => $weight, + 'name' => $url, + // Setting the name above may appear superfluous, but it preserves a historical quirk + // where Region::add() and Resources::addScriptUrl() produce slightly different orderings.. + ]); return $this; } diff --git a/CRM/Core/Resources/CollectionTrait.php b/CRM/Core/Resources/CollectionTrait.php index 6532d752a3..eca138278d 100644 --- a/CRM/Core/Resources/CollectionTrait.php +++ b/CRM/Core/Resources/CollectionTrait.php @@ -292,6 +292,42 @@ trait CRM_Core_Resources_CollectionTrait { return $this; } + /** + * Add a JavaScript file to the current page using