Jump to content

Find page name field in non default language.


elabx
 Share

Recommended Posts

I built a menu using Pages to let the translation handle itself through PW multi language page title fields. Though, while trying some click menu and scroll to element with class gimmicky, I decided to try to place a data attribute inside the clicked link so that I can later use it for the javascript class selection.

  $navigationPages =  $pages->get("/navigation/")->children();
	  $spanish = $user->language->id;
	  foreach ($navigationPages as $navigationItem){
		  echo "<li><a data-scroll='{$navigationItem->name}'>{$navigationItem->title} </a></li>";
	  }

So while trying this, as you can see I used the name property to echo the data attribute value, I stupidly kept trying to echo the name in the non default language (spanish), until I decided to not break my brain and just use the one given by default (english).

Could I have actually output the name in the non default language?

For example I tried:

$navigationItem->name->getLanguageValue($spanish)

But didn't work, and I'm just wondering why. 

And I am also wondering, is this and this the only documentation available for using the language support through the API? I was just a bit surprised I didn't quite find any reference of this on the cheatsheet, is this because $languages or $user->language have to do with the language modules?

Should I start reading source code to find out about this? 

  • Like 1
Link to comment
Share on other sites

Turn off output formatting first. As name is a string and not an object when output formatting is on.

Or try $page->getUnformatted(fieldname)->getLanguageValue(lang)

Or just this as it's a page native field..

$page->get("name$langId")

Link to comment
Share on other sites

Turn off output formatting first. As name is a string and not an object when output formatting is on.

Or try $page->getUnformatted(fieldname)->getLanguageValue(lang)

Or just this as it's a page native field..

$page->get("name$langId")

This worked nice! 

$page->get("name$langId") 

Though if I set it like this, I have kind of save first the ID of the language I will use to output the data. It calls to my attention that when set on the default language, it seems that "name$langID" doesn't do anything. 

I will try the other lines of code later more carefully, 'cause I kind of quickly tested your suggestions.

Thanks a lot for your answer!

Link to comment
Share on other sites

It's because default language does have an id, but the field for the default language doesn't use it.

So you have:

- lang: default, id: 1017, field: name

- lang: other, id: 1019, field: name1019

- lang: third, id: 1020, field: name1020

So, if you just tried get("name$langId") for default language, you'd try to access "name1017", which doesn't exist.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

This thread is already quite old, but I just stumbled upon the same problem and wanted to add my solution to this thread.

When you install the LanguageSupportPageNames module, you also get the Page::localName($language) method to easily retrieve the page name in different languages. This has been added in 2.3.0

So, to get the page name in a the language (or any other), you could do something around these lines and be done:

// Get default language
$defaultLanguage = $languages->getDefault();

// Get default page name
$defaultName = $page->localName($defaultLanguage);
  • 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...