Zeka Posted January 3, 2018 Share Posted January 3, 2018 Very often I have the same page name and title for all languages in ML context for some pages. So I create the page and only set title for default language (My title field is set to "Inherit from default language when blank, so it is ok to leave it blank). Then if you look at "Settings" tab you will see that name filed for non-default language is also filled with the value of default language. UI tells that I have names for this page in both languages, but in the DB you will see the next: Another way to get the same thing is to create a page with filled titles for both languages, save it, go to "Settings" tab and clear name field for non-default language and then save the page. UI will tell that you have page names for both languages, but in DB there will be the same picture as in the first example. Even if you manually enter the same name as in default language it will not get saved in DB. This behavior doesn't look intuitive for me and raises some issues. For example, this code will not work for these pages if(input()->urlSegment2) throw new Wire404Exception(); if(input()->urlSegment1) { $pagename = input()->urlSegment(1); $language = user('language'); $nameFieldSelector = $language->isDefault() ? "name" : "name" . $language; bd($nameFieldSelector); $match = pages()->findOne("template=blog-category|blog-item, $nameFieldSelector={$pagename}"); bd($match); if(!$match->id) throw new Wire404Exception(); echo $match->render(); return $this->halt(); } And this also will return a blank line. $language = $user->language; $pageName = $page->localName($language); // "" Of course there are workarounds for these particular cases but the current behavior of ML name field makes it not clear in some situation do I have a name for language or I don't. What do you think about it? How it possible to improve UI part? Should I raise issue for it? Link to comment Share on other sites More sharing options...
Soma Posted January 4, 2018 Share Posted January 4, 2018 If the name is the same on alternative languages, PW doesn't store it cause it's redundant data. The first code is wrong, as there's only "name" in a find selector not a "name[LANGID]". You don't need to handle the language as it's handled by the user language already. The second code needs a second param to be "true" to get the default name if the localized is empty. $page->localName($lang, true). /** * Add a Page::localName function with optional $language as argument * * event param Language|string|int|bool Optional language, or boolean true for behavior of 2nd argument. * event param bool Substitute default language page name when page name is not defined for requested language. * event return string Localized language name or blank if not set * * @param HookEvent $event * */ 1 1 Link to comment Share on other sites More sharing options...
Zeka Posted May 20, 2018 Author Share Posted May 20, 2018 Hi @Soma. Thank you for your answer. I still have the issue that this code doesn't find the page with non-default language if(input()->urlSegment2) throw new Wire404Exception(); if(input()->urlSegment1) { $pagename = input()->urlSegment(1); $language = user('language'); $match = pages()->get("template=blog-category|blog-item, name={$pagename}"); if(!$match->id) throw new Wire404Exception(); echo $match->render(); return $this->halt(); } but this finds if(input()->urlSegment2) throw new Wire404Exception(); if(input()->urlSegment1) { $pagename = input()->urlSegment(1); $language = user('language'); $nameFieldSelector = $language->isDefault() ? "name" : "name" . $language; $match = pages()->get("template=blog-category|blog-item, $nameFieldSelector={$pagename}"); if(!$match->id) throw new Wire404Exception(); echo $match->render(); return $this->halt(); } Do you have any idea what could be the reason? Link to comment Share on other sites More sharing options...
Soma Posted May 21, 2018 Share Posted May 21, 2018 For me the first code works perfectly also for alternative languages, but only if you're viewing the correct language ofc. The second example one doesn't work for alternative language with names that are "empty" in DB. 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