Jump to content

$pages->get() does not work properly with multi-language home page


Recommended Posts

Hi, I am currently using Processwire for a client project and am quite pleased with the ease of use and versatility.

Unfortunately I came across a problem I am not able to solve:

I have a multi-lang site where the home page path are as follows:
www.example.com/ -> german (default)
www.example.com/en/ -> english
...

I build a language switcher that first gets the current page and then displays links for the page in all the different languages.

<?php namespace ProcessWire; ?>
<ul class="lang-switcher">
<?php
$real_page = $pages->get($_SERVER['REQUEST_URI']);

foreach ($languages as $language) {
	if (!$real_page->viewable($language)) {
        continue;
    }
	// echo <li>...</li>
}
 ?>
</ul>

(I need to search for the real page because the language switcher is loaded inside of the menu which is an additional page/template which get loaded via

echo $pages->get("title='Main menu'")->render();

. When using $page->localURL() I get the /main-menu page.)

this works without problems for urls:
www.example.com/
www.example.com/test
www.example.com/en/test
...

But not for:
www.example.com/en/

I diagnosed that $pages->get("/en/") returns a NullPage instead of the english home page.

Do you have any idea why this is and how to fix it.

Thanks alot.

Link to comment
Share on other sites

Hi @digitalhandwerker

In your scenario, where you are getting pages by their paths would be more appropriate to use $page->getByPath() method like

$pages->getByPath('/en/', [
  'useLanguages' => true,
]);

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...