Jump to content

[NOOB QUESTION] How to select a page in multi language environment?


Gadgetto
 Share

Recommended Posts

What is the best way to select a page in a multi-language environment?

Explanation:

The site I'm working on has 2 languages - EN, DE

I have a page with EN name "sample-page" and DE name "beispiel-seite".

How can I select this page language-independent via $page variable?

Thanks for your help!
Martin

  • Like 1
Link to comment
Share on other sites

@Zeka Thank you for jumping in again!

Let's say I'd like to output the title field and the summary field from the "sample-page" (DE name -> "beispiel-seite") on the "home" page (maybe in a sidebar).

How would I reference the content of these fields?

 

  • Like 1
Link to comment
Share on other sites

You can do it multiple ways and that really depends on you! I will assume for now, that sidebar content wants to bring in some info from the articles section of your website. Lets say you get the idea to use the structure of the page tree to organise content and make it easy to get those articles to show on home page.

Quote

 

- Home 

-- Articles (template=articles)

--- Awesome article 1 (template=article)

 

So, you could pull something like this to get ten articles which are identified with the template "article" (that has title and summary fields!):

$pages->find("template=article, limit=10");

Another way, could be to get all pages, whose parent has the template articles (for convenience, this template is used once throughout the whole site, to handle this parent page):

$pages->find("parent.template=articles, limit=10")

And another one to ask for the first page found with the template articles, and then get the first 10 children

$pages->get("template=articles")->children("limit=10");

Which is the best? It's your call! Will depend on a lot of things, just to give an example, on the last option to get the articles, I am assuming all pages under Articles, are actually articles. What if a page that is not an article exists under there?? Maybe something like:

Quote

 

-- Articles

---- Article 1

---- Article 2

--- Categories

---- Category 1

---- Category 2

 

 

  • Like 2
Link to comment
Share on other sites

@elabx Thank you for your extensive answer!

But my question was about how to get a specific page when there are multiple languages. The name field wont work as it has different content for each language.

Seems I need to uses the id field in this case (which doesn’t seem to be the best way).

  • Like 1
Link to comment
Share on other sites

15 hours ago, Gadgetto said:

I have a page with EN name "sample-page" and DE name "beispiel-seite".

How can I select this page language-independent via $page variable?

I have to support the existing anwers. It totally depends on you what the best option for selecting the page is. And the most important part of that question is WHY you want to select this page...

  • Is the page a settings page? Then it might be the best to select it via ID (changing names to not break your code)
  • Is the page part of some listing (eg list all news items on the news overview page --> select it via children/parent or via template, eg $pages->children(), $pages->find('template=newsitem');
  • Many other scenarios, so your explanation is a bit too general imho ? 
  • Like 2
Link to comment
Share on other sites

@Gadgetto

There is no problem with getting pages with the name field. 

Just use default language value as selector value. 

Getting page by name has some pitfalls, $pages->get() return the first found page, but you can have several pages with the same name under different parents, so you should specify parent selector to make sure that you are looking for the needed page. 

So basically you can use this in English and Deutsch languages

d($lang->title); // english
d($pages->get("name=sample-page")->title); // Sample page
$user->language = $languages->get('deutsch'); // change user language to deutsch
d($pages->get("name=sample-page")->title); // beispiel-seite

But if you want to find your page by Deutsch name:

$lang = $languages->get('deutsch'); get deutsch language
d($lang->title); // deutsch
d($user->language->title); // english
d($pages->get("name{$lang}=beispiel-seite")->title); // beispiel-seite
  • Like 3
Link to comment
Share on other sites

12 minutes ago, Zeka said:

@Gadgetto


d($lang->title); // english
d($pages->get("name=sample-page")->title); // Sample page
$user->language = $languages->get('deutsch'); // change user language to deutsch
d($pages->get("name=sample-page")->title); // beispiel-seite

But if you want to find your page by Deutsch name:


$lang = $languages->get('deutsch'); get deutsch language
d($lang->title); // deutsch
d($user->language->title); // english
d($pages->get("name{$lang}=beispiel-seite")->title); // beispiel-seite

This (and selecting by id) seems to be the best answers for my question! Thank you @Zeka and all others for your help! I've heard before that the Processwire community is very helpful and friendly and I have to say that's absolutely true! 

I'm just trying to understand the basics of Processwire. Hence my seemingly pointless questions.

I come from a different CMS (MODX) and it's a bit difficult to rethink at first.

So I hope you'll also excuse my upcoming stupid questions... ?

 

@bernhard Hey, I'm also from Austria - only 60 km from Vienna, in  Mattersburg.

  • Like 3
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...