Hi there, so I have this multilanguage website and I think I must be missing something, because everything else works beautifully.
I have a temporary.php template both in el and en languages.
<h1><?= $page->title() ?></h1>
<p><?= $page->text() ?></p>
<?php dump(page('commonstuff')->contactdetails()->yaml()) ?>
<?= $page->buttonURL()->toPage() ?>
So the first two lines work exactly how I would imagine, i.e. title and text come from the temporary.el.txt and temporary.en.txt depending from where I visit. The third line also works correctly, since it gets the contact details from commostuff.el.txt when I visit /el/temporary.
The fourth line returns always the page url without the appropriate /lang/ prefix.
Is it how it’s supposed to work?
And if yes, is there a common way to include the language prefix?
(I guess I could <?php echo $kirby->language()->url() ?> before each link)
What is the buttonURL field, a pages field? And what is your expected output? Because currently your code for that lines doesn’t really make sense. You convert to a page, but echoing a page object is pretty useless.
Yes, I must be missing something, so let me explain what I had in mind:
It’s a pages field, so for instance the user can choose from the panel which page a button should lead to. I want the link that will go into the href to be multi-lang (if it’s possible).
The correct code would have to be
<?php if ( $linkPage = $page->buttonURL()->toPage() ) : ?>
<?php echo $linkPage->url() ?>
<?php endif; ?>
Or as a one-liner:
<?= ( $linkPage = $page->buttonURL()->toPage() ) ? $linkPage->url() : '' ?>
Yes, thank you! Sending virtual hug!
It don’t know why that works, but it does work. I’ll study it more on my own time and get back to you, if I have more questions.
Thanks again texnixe for the excellent support!
toPage() you convert your field value into a page object. Once you have made sure this page object exists (that’s what we use the if statement for), you can use it in the same way you usually work with any other page, i.e. you can call all the available page methods ( https://getkirby.com/docs/reference/objects/pag) on this page object.
If you want a simple intro about objects and classes, check out my OOP recipe: A brief intro to object oriented programming in PHP | Kirby
If you echo your page object like you did before, this works because of the magic
__toString() method, which simply returns the id.
Brilliant! Thanks a lot – I’ll check the cookbook recipe as well!