diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d9ced647..0ed573181 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.1.0-rc.2 ## xx/xx/2016 +1. [](#new) + * Added getters and setters for Assets to allow manipulation of CSS/JS/Collection based assets via plugins [#876](https://github.com/getgrav/grav/issues/876) 1. [](#improved) * Moved list items in `system/config/media.yaml` config into a `types:` key which allows you delete default items. diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index cf3cece0f..923c05636 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -821,24 +821,94 @@ class Assets /** * Return the array of all the registered CSS assets + * If a $key is provided, it will try to return only that asset + * else it will return null * + * @param null|string $key the asset key * @return array */ - public function getCss() + public function getCss($key = null) { + if (!empty($key)) { + $asset_key = md5($key); + if (isset($this->css[$asset_key])) { + return $this->css[$asset_key]; + } else { + return null; + } + } + return $this->css; } /** * Return the array of all the registered JS assets + * If a $key is provided, it will try to return only that asset + * else it will return null * + * @param null|string $key the asset key * @return array */ - public function getJs() + public function getJs($key = null) { + if (!empty($key)) { + $asset_key = md5($key); + if (isset($this->js[$asset_key])) { + return $this->js[$asset_key]; + } else { + return null; + } + } + return $this->js; } + /** + * Set the whole array of CSS assets + * + * @param $css + */ + public function setCss($css) + { + $this->css = $css; + } + + /** + * Set the whole array of JS assets + * + * @param $js + */ + public function setJs($js) + { + $this->js = $js; + } + + /** + * Removes an item from the CSS array if set + * + * @param $key the asset key + */ + public function removeCss($key) + { + $asset_key = md5($key); + if (isset($this->css[$asset_key])) { + unset($this->css[$asset_key]); + } + } + + /** + * Removes an item from the JS array if set + * + * @param $key the asset key + */ + public function removeJs($key) + { + $asset_key = md5($key); + if (isset($this->js[$asset_key])) { + unset($this->js[$asset_key]); + } + } + /** * Return the array of all the registered collections * @@ -849,6 +919,16 @@ class Assets return $this->collections; } + /** + * Set the array of collections explicitly + * + * @param $collections + */ + public function setCollection($collections) + { + $this->collections = $collections; + } + /** * Determines if an asset exists as a collection, CSS or JS reference *