Skip to content

Commit

Permalink
Fixes for multilang taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Mar 21, 2024
1 parent 26a6cb7 commit afb5b02
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 15 additions & 10 deletions system/src/Grav/Common/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,6 +38,8 @@ class Taxonomy
protected $taxonomy_map;
/** @var Grav */
protected $grav;
/** @var Language */
protected $language;

/**
* Constructor that resets the map
Expand All @@ -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()] = [];
}

/**
Expand Down Expand Up @@ -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()];
}
}

Expand All @@ -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] ?? [];
}
}

Expand All @@ -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] ?? [];
}

/**
Expand All @@ -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]) : [];
}
}

0 comments on commit afb5b02

Please sign in to comment.