Jump to content

Using name-field on multi language site


Marcel Stäheli
 Share

Recommended Posts

Hello

if have a few article pages that have a multi-page reference field with tags about the content of the article. All tags of a given page are displayed and link to a tag-overview page.

Every link has a query string added with the tag name like &tag=cars so that I know which articles have to be displayed on the tag-overview page,
The tag name is created using $tag->name. Since the website is multi language using German (default) and English, I use $page->localName($language) to get the localized page name . The code looks like this:

foreach($tags as $tag) {        
    $tagsMarkup .= "<a  href='" .
                    wire('pages')->get('/tags-overview/')->url . 
                    "?tag=" . $tag->localName(wire('user')->language) . 
                    "'>" .  $tag->title . "</a>, ";
}


The code works mostly fine, but when a page has the same name in both languages, the query string will have no value just ?tag=. Why?

 

A second problem that might be tied to the first:
Once on that tags-overview page, I check if the tag actually exists using:
 

$allTags = $pages->get("/tags/")->children);

if($allTags->has("name=$tag") {...}

But as I read on the forum (https://processwire.com/talk/topic/2979-multi-language-page-names-urls/?page=6) that only checks for name in the default language. It was suggested to use $page->parent->path . "pageNameEnglish" but I'm not sure where and how to use it in my case.

Any help?

 

Link to comment
Share on other sites

@Zeka Thanks for testing it. I was not precise enough. It works with the default language German. But when I switch to English the query string of a page with the same name in both languages is &tag=. Did you check in both languages?

Second question: It is meant as a security check. I noticed that should someone enter a query string manually like &tag=a I get all kinds of pages listed. So I make sure that the tag actually exists. There are probably better ways to do this. But I would still be interested to know what would be the easiest way to use find() to search for name-field values in the current language?

Link to comment
Share on other sites

@Marcel Stäheli Actually I checked whether I get value from $page->localName($user->langauge); I checked it in three languages and even with activating/deactivating it in page settings. So even if a page is not active for the current language I still got language specific name.

Second:

Why not use

$tagName = $sanitizer->name($tag)

if($pages->get("/tags/{$tagName}"}->id) {
	...
}

If I'm not mistaken it looks for in all languages. 

 

Link to comment
Share on other sites

  • 3 weeks later...

@Zeka I forgot to answer, sorry.

I solved the first problem: On pages with the same page name in both languages, I only entered a page name in the default language. Normally PW falls back to that name if no name is entered in another language. However if you use localName it doesn't. I just had to enter the same page name in both languages.

Second problem: I couldn't get your code to work (I fixed the  ')' instead of the '}' ). Something is wrong so I just tried to do it without checking if the tag exists. But I can't get that to work either:

$articles = $pages->find("template=article,tag_select=$tagName");

This is supposed to find any article page where the multi-page reference field contains a page with the name $tagName in any language, right? It works only with the default language. As soon as I switch the language, no results. The only tags where I get results are the ones that have the same name in both languages. Since tag_select is a page-reference field, is it the same problem as before? If you search for pages with a certain name it only searches in the default language? If so, how could I search for say page titles within the page reference field?

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...