Jump to content

Get value in other language (not default) if field value is empty


nabo
 Share

Recommended Posts

Hello

I manage a multi-language site. In very few case I would like to have a content in other language than default when the value is not set in current language.

I try this way to check if the value is empty, but the value is printed with default language

$cl = $languages->get($user->language->name);
$blog_title = $page->getLanguageValue($cl, 'title');

Is there a way to check if a value is empty in some language?

Link to comment
Share on other sites

24 minutes ago, virtualgadjo said:

have you tried checking the "remain blank" option for the fields you want to work this way as, by default, it's the "inherit value from default language" that is checked?

Yes, I know this feature but I can't change this setting for these fields ?

Link to comment
Share on other sites

Hi,

too bad, if you can't change it because it's used in other circumstances for the given field, the simplest way i can imagine is, when you need a different behaviour, in your template, just check if the value for a given language is the same as the default language one and, if so, just grab the value from another language, a bit savage but it should work ?

have a nice day

Link to comment
Share on other sites

2 hours ago, virtualgadjo said:

Hi,

too bad, if you can't change it because it's used in other circumstances for the given field, the simplest way i can imagine is, when you need a different behaviour, in your template, just check if the value for a given language is the same as the default language one and, if so, just grab the value from another language, a bit savage but it should work ?

have a nice day

This is the problem! I can't check because the value for a given language (if the value is empty) is returned with default value. 

Link to comment
Share on other sites

that's why what i suggested is to
- grab the default language value
- then grab the current language value
- comparing them both, if the current one is the same as the default one, the value for this language is empty as if it weren't both values would be different ?

if they are identical, up to you to do what you want ? and pw is so fast, this shouldn't be too consuming

have a nice day

Link to comment
Share on other sites

Can you not do this?

$cl = $languages->get($user->language->name);
$blog_title = empty($page->getUnformatted('title')->getLanguageValue[$cl])
            ? 'Look Ma No Title' //user’s language title is empty
            : $page->getLanguageValue($cl, 'title'); //user’s language title is not empty

The unformatted value of language fields should be a LanguagesPageFieldValue object that you can ask about language values without fallback: https://github.com/processwire/processwire/blob/dev/wire/modules/LanguageSupport/LanguagesPageFieldValue.php

  • Like 1
  • Thanks 2
Link to comment
Share on other sites

2 minutes ago, Jan Romero said:

Can you not do this?

$cl = $languages->get($user->language->name);
$blog_title = empty($page->getUnformatted('title')->getLanguageValue[$cl]) ? 'Look Ma No Title' : $page->getLanguageValue($cl, 'title');

The unformatted value of language fields should be a LanguagesPageFieldValue object that you can ask about language values without fallback: https://github.com/processwire/processwire/blob/dev/wire/modules/LanguageSupport/LanguagesPageFieldValue.php

Thank you!!!!!

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