From 4752324d3148f1add567cb01a44e0dc0161ac2b0 Mon Sep 17 00:00:00 2001 From: Karmalakas Date: Sat, 30 Jan 2021 14:39:59 +0200 Subject: [PATCH] Add method to get route by language Just an idea on how [redirect problem](https://github.com/getgrav/grav/issues/3180) might be fixed maybe. Or at least partially. Although this doesn't work - couldn't figure out how to get parent. Without the parent, `route()` returns just a home route. Maybe someone could take this over, or tell me the approach is completely wrong :) But that issue really needs to be fixed. Otherwise I'll have to search for alternatives and I really like Grav otherwise, so it would be sad if I couldn't publish my page and had to ditch a few weeks work :( Also `Page::init()` calls `$this->id($this->modified() . md5($this->filePath()))`, but it gets overwritten, because `$this->id` doesn't exist yet, so added a check in `Page::id()` --- system/src/Grav/Common/Page/Page.php | 52 +++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 344a7ba60c..f03dda6699 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -91,6 +91,8 @@ class Page implements PageInterface protected $url; /** @var array */ protected $routes; + /** @var array */ + protected $languageRoutes; /** @var bool */ protected $routable; /** @var int */ @@ -1905,6 +1907,54 @@ public function route($var = null) return $this->route; } + /** + * @param string|null $language + * @return string + */ + public function languageRoute(?string $language = null): string + { + if ($language === null || !array_key_exists($language, $this->translatedLanguages())) { + return $this->route(); + } + + if (!empty($this->languageRoutes[$language])) { + return $this->languageRoutes[$language]; + } + + $currentLanguage = trim(basename($this->extension, 'md'), '.') ?: null; + + if ($language === $currentLanguage) { + return $this->route(); + } + + $excludeLanguage = false; + $defaultLanguage = Grav::instance()['language']->getDefault(); + + if ($language === $defaultLanguage) { + $includeDefaultLanguage = (bool)Grav::instance()['config']->get('system.languages.include_default_lang_file_extension'); + $excludeLanguage = !$includeDefaultLanguage; + } + + $path = sprintf('%1$s%2$s%3$s%2$s', $this->path, DS, $this->folder); + $name = str_replace( + sprintf('.%s.md', $currentLanguage), + $excludeLanguage ? '.md' : sprintf('.%s.md', $language), + $this->name + ); + + $filePath = sprintf('%s%s', $path, $name); + + if (file_exists($filePath)) { + $page = new Page(); + $page->parent(new Page()); + $page->init(new \SplFileInfo($filePath)); + + return $page->route(); + } + + return $this->route(); + } + /** * Helper method to clear the route out so it regenerates next time you use it */ @@ -1986,7 +2036,7 @@ public function id($var = null) { if (null === $this->id) { // We need to set unique id to avoid potential cache conflicts between pages. - $var = time() . md5($this->filePath()); + $var = $var ?? time() . md5($this->filePath()); } if ($var !== null) { // store unique per language