* @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;
}
* @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;
}
* @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;
}
* @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;
}
* 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 {
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;
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;
+ }
+
}