Have this custom route setup that is getting a URL from a panel field. I use this tutorial to set up a route that takes in a field from the panel: Routes | Kirby CMS. It needs to be translated, but the current code I am using is not returning the correct translation:
It is an external URL, so the second approach is the more accurate one. The page has to be dynamic as this is used for many different pages, that is why the URI was being pulled in before.
I tried using this:
'pattern' => 'redirect/(:all)',
'language' => '*',
'action' => function ($language, $uri) {
// if ($page = page($uri) ) {
$page = page($uri);
$lang = $language()->code();
$url = $page->content($lang)->ref_url();
// assuming we're storing a full external url in 'ref_url':
go($url);
}
// }
I think you’re getting that error because the line should be $language->code(), not $language()->code() - ie., $language is an object, not a function/method (that’s my mistake!).
I also suspect that the $page can be magically passed by Kirby to your function, so you might be able to do this:
'action' => function ($language, $page) {
$lang = $language->code();
$url = $page->content($lang)->ref_url();
// assuming we're storing a full external url in 'ref_url':
go($url);
}
I found the source of the problem. This whole setup is to change the URL of some links from full external URLs to something like domain.com/redirect/page-name. The redirect for the page is coming from a page model which adds the redirect path:
<?php class BonusPage extends Page {
public function cloakedUrl() {
return url('redirect/' . $this->id());
}
} ?>
However, in the model, the translation is never passed, so it’s always going to the default one.
Think I may have found a workaround, it seems like the correct translation is not being passed to the config.php router, but if I add the correct translation to the cloakedURL function:
public function cloakedUrl() {
return url(kirby()->language()->code() . '/redirect/' . $this->id());
}
It does return the correct translation when I dump the language in the config:
The only issue here is that it is returning two languages, the default one and the correct one.
The documentation for translating page models is quite limited. How can I pass the correct translation into the config.php? Or do you think there would there be a better way to do this?