From afb5b02e5750f0f9c0bcc73de5d3f62947881722 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 21 Mar 2024 15:08:44 -0600 Subject: [PATCH] Fixes for multilang taxonomy --- CHANGELOG.md | 6 ++++++ system/src/Grav/Common/Taxonomy.php | 25 +++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 014bff7d51..d9443babb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v1.7.46 +## mm/dd/2024 + +1. [](#bugfix) + * Fixes for multi-lang taxonomy when reinitializing the languages (e.g. LangSwitcher plugin) + # v1.7.45 ## 03/18/2024 diff --git a/system/src/Grav/Common/Taxonomy.php b/system/src/Grav/Common/Taxonomy.php index d9cb930ba7..3ce2173170 100644 --- a/system/src/Grav/Common/Taxonomy.php +++ b/system/src/Grav/Common/Taxonomy.php @@ -10,6 +10,7 @@ namespace Grav\Common; use Grav\Common\Config\Config; +use Grav\Common\Language\Language; use Grav\Common\Page\Collection; use Grav\Common\Page\Interfaces\PageInterface; use function is_string; @@ -37,6 +38,8 @@ class Taxonomy protected $taxonomy_map; /** @var Grav */ protected $grav; + /** @var Language */ + protected $language; /** * Constructor that resets the map @@ -45,8 +48,9 @@ class Taxonomy */ public function __construct(Grav $grav) { - $this->taxonomy_map = []; $this->grav = $grav; + $this->language = $grav['language']; + $this->taxonomy_map[$this->language->getLanguage()] = []; } /** @@ -107,7 +111,8 @@ public function iterateTaxonomy(PageInterface $page, string $taxonomy, string $k if (!empty($key)) { $taxonomy .= $key; } - $this->taxonomy_map[$taxonomy][(string) $value][$page->path()] = ['slug' => $page->slug()]; + $active = $this->language->getLanguage(); + $this->taxonomy_map[$active][$taxonomy][(string) $value][$page->path()] = ['slug' => $page->slug()]; } } @@ -123,14 +128,11 @@ public function findTaxonomy($taxonomies, $operator = 'and') { $matches = []; $results = []; + $active = $this->language->getLanguage(); foreach ((array)$taxonomies as $taxonomy => $items) { foreach ((array)$items as $item) { - if (isset($this->taxonomy_map[$taxonomy][$item])) { - $matches[] = $this->taxonomy_map[$taxonomy][$item]; - } else { - $matches[] = []; - } + $matches[] = $this->taxonomy_map[$active][$taxonomy][$item] ?? []; } } @@ -156,11 +158,13 @@ public function findTaxonomy($taxonomies, $operator = 'and') */ public function taxonomy($var = null) { + $active = $this->language->getLanguage(); + if ($var) { - $this->taxonomy_map = $var; + $this->taxonomy_map[$active] = $var; } - return $this->taxonomy_map; + return $this->taxonomy_map[$active] ?? []; } /** @@ -171,6 +175,7 @@ public function taxonomy($var = null) */ public function getTaxonomyItemKeys($taxonomy) { - return isset($this->taxonomy_map[$taxonomy]) ? array_keys($this->taxonomy_map[$taxonomy]) : []; + $active = $this->language->getLanguage(); + return isset($this->taxonomy_map[$active][$taxonomy]) ? array_keys($this->taxonomy_map[$active][$taxonomy]) : []; } }