diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aedeba4f..96b5813b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Added new `onTwigLoader()` event to enable utilization of loader methods * Added new `Twig::addPath()` and `Twig::prependPath()` methods to wrap loader methods and support namespacing [#1604](https://github.com/getgrav/grav/issues/1604) * Added new `array_key_exists()` Twig function wrapper + * Added a new `Collection::intersect()` method [#1605](github.com/getgrav/grav/issues/1605) 1. [](#bugfix) * Allow `session.timetout` field to be set to `0` via blueprints [#1598](https://github.com/getgrav/grav/issues/1598) * Fixed `Data::exists()` and `Data::raw()` functions breaking if `Data::file()` hasn't been called with non-null value diff --git a/system/src/Grav/Common/Page/Collection.php b/system/src/Grav/Common/Page/Collection.php index 121d1a350..386b93134 100644 --- a/system/src/Grav/Common/Page/Collection.php +++ b/system/src/Grav/Common/Page/Collection.php @@ -63,6 +63,20 @@ public function addPage(Page $page) return $this; } + /** + * Add a page with path and slug + * + * @param $path + * @param $slug + * @return $this + */ + public function add($path, $slug) + { + $this->items[$path] = ['slug' => $slug]; + + return $this; + } + /** * * Create a copy of this collection @@ -89,6 +103,23 @@ public function merge(Collection $collection) return $this; } + /** + * Intersect another collection with the current collection + * + * @param Collection $collection + * @return $this + */ + public function intersect(Collection $collection) + { + $array1 = $this->items; + $array2 = $collection->toArray(); + + $this->items = array_uintersect($array1, $array2, function($val1, $val2) { + return strcmp($val1['slug'], $val2['slug']); + }); + return $this; + } + /** * Set parameters to the Collection * diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index a5575e26f..d6f417376 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -9,6 +9,7 @@ namespace Grav\Common\Twig; use Grav\Common\Grav; +use Grav\Common\Page\Collection; use Grav\Common\Page\Media; use Grav\Common\Utils; use Grav\Common\Markdown\Parsedown; @@ -94,6 +95,7 @@ public function getFilters() new \Twig_SimpleFilter('truncate_html', ['\Grav\Common\Utils', 'truncateHTML']), new \Twig_SimpleFilter('json_decode', [$this, 'jsonDecodeFilter']), new \Twig_SimpleFilter('array_unique', 'array_unique'), + ]; } @@ -108,6 +110,7 @@ public function getFunctions() new \Twig_SimpleFunction('array', [$this, 'arrayFunc']), new \Twig_SimpleFunction('array_key_value', [$this, 'arrayKeyValueFunc']), new \Twig_SimpleFunction('array_key_exists', [$this, 'arrayKeyExistsFunc']), + new \Twig_SimpleFunction('array_intersect', [$this, 'arrayIntersectFunc']), new \Twig_simpleFunction('authorize', [$this, 'authorize']), new \Twig_SimpleFunction('debug', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]), new \Twig_SimpleFunction('dump', [$this, 'dump'], ['needs_context' => true, 'needs_environment' => true]), @@ -832,6 +835,23 @@ public function arrayKeyExistsFunc($key, $current_array = null) return array_key_exists($key, $current_array); } + /** + * Wrapper for array_intersect() method + * + * @param $array1 + * @param $array2 + * @return array + */ + public function arrayIntersectFunc($array1, $array2) + { + if ($array1 instanceof Collection && $array2 instanceof Collection) { + return $array1->intersect($array2); + } else { + return array_intersect($array1, $array2); + } + + } + /** * Returns a string from a value. If the value is array, return it json encoded *