Gadgetto Posted November 3, 2018 Share Posted November 3, 2018 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 1 Link to comment Share on other sites More sharing options...
Zeka Posted November 3, 2018 Share Posted November 3, 2018 @Gadgetto What do you mean with 'select a page'? Does this section answer your question? https://processwire.com/api/multi-language-support/multi-language-fields/#how-language-fields-work 2 Link to comment Share on other sites More sharing options...
Gadgetto Posted November 3, 2018 Author Share Posted November 3, 2018 @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? 1 Link to comment Share on other sites More sharing options...
Zeka Posted November 3, 2018 Share Posted November 3, 2018 Ok. First of all, make sure that you know how selector works in PW. https://processwire.com/api/selectors/ You can get a page by its id: $some_page = $pages->get(2030); // get page by id echo $some_page->title; echo $some_page->summury; 1 Link to comment Share on other sites More sharing options...
Gadgetto Posted November 3, 2018 Author Share Posted November 3, 2018 Getting the page by it's id was my first thought, but is this the best way? The id field isn't even visible in PW, only in browsers URL... 1 Link to comment Share on other sites More sharing options...
elabx Posted November 4, 2018 Share Posted November 4, 2018 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 2 Link to comment Share on other sites More sharing options...
Gadgetto Posted November 4, 2018 Author Share Posted November 4, 2018 @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). 1 Link to comment Share on other sites More sharing options...
bernhard Posted November 4, 2018 Share Posted November 4, 2018 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 ? 2 Link to comment Share on other sites More sharing options...
Zeka Posted November 4, 2018 Share Posted November 4, 2018 @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 3 Link to comment Share on other sites More sharing options...
Gadgetto Posted November 4, 2018 Author Share Posted November 4, 2018 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. 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now