Jump to content

Processing fields for multi language forms


gebeer
 Share

Recommended Posts

Hello,

I am building a dynamic frontend form from fields of a page, following Soma's great gist example.

The form is working fine, except for multi language values.

After this part of the code

$form->processInput($this->input->post)

when I loop over the form fields, I don't know how to access the multi language values of a field

foreach($form as $field) {
    // how to get multi lang values here
}

What I tried is not working

        foreach($form as $field) {
        	if($field->type instanceof FieldtypeLanguageInterface) {
        		foreach ($languages as $lang) {
        			$langval = $editpage->$field->getLanguageValue($lang);
        			$editpage->$field->setLanguageValue($lang, $langval);
        		}
        	} else {
	            $editpage->set($field->name, $field->value);
        	}
        } 

Actually the $field->type returns just "text" for a PageTitleLanguage field.

var_dumping the $field reveals that the language values are stored deep inside the object like

      'value1023' => string 'Testworkshop2' (length=13)
      'value1032' => string 'Testworkshop2 Englisch' (length=22)

So how would I access those other than pulling them directly from $this->input->post?

Link to comment
Share on other sites

I think the problem is with $form->processInput($this->input->post) not taking into account multilanguage values.

get_class($editpage->title) returns: LanguagesPageFieldValue

But inside the foreach($form as $field) {...} a var_dump(get_class($field)) for the title field returns: InputfieldPageTitle

These are 2 different types of objects. I would expect to get a LanguagesPageFieldValue object where I can access multi language values. But sadly this is not the case.

Would be nice to have a method like processInputLanguage().

I have searched the code and the forum but cannot find a clue how to deal with multiliangugae fields in form processing.

Any help would be much appreciated.

Link to comment
Share on other sites

The docs says:

$page->of(false); // outputFormatting must be OFF

https://processwire.com/api/multi-language-support/multi-language-fields/#getting-and-setting

"The value of a multi-language or language-alternate field is affected by the "outputFormatting" setting of a page. On the front-end of your site, outputFormatting is always turned on, unless you've turned it off."

"When outputFormatting is ON, the value of a multi-language field is a string (as it shows in most examples on this page). That string reflects the value from the current user's language. If the value is blank in the current user's language, the value from the default language will be there instead.

However, when outputFormatting is OFF, the value of a multi-language field (like $page->body) will instead return an object of type LanguagesPageFieldValue, rather than a string. That object contains all of the translations available for the given field."
 

I'm not sure that this solves anything, I'm just guessing. Sorry if I'm mistaken.

  • Like 3
Link to comment
Share on other sites

Thank you for taking the time to look into this.

Unfortunatley this doesn't apply to my situation because I already switch off output formatting right before the foreach loop

        $editpage->setOutputFormatting(false);
        foreach($form as $field) {
            // here is where I need to get the multi language values
            $editpage->set($field->name, $field->value);
        }
  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Raising this from the dead, but setLanguageValue() and getLanguageValue() are pretty useful for those kind of things.

From code/Page.php
 
 * Methods added by LanguageSupport.module (not installed by default) 
 * ------------------------------------------------------------------
 * @method Page setLanguageValue($language, $fieldName, $value) Set value for field in language (requires LanguageSupport module). $language may be ID, language name or Language object.
 * @method Page getLanguageValue($language, $fieldName) Get value for field in language (requires LanguageSupport module). $language may be ID, language name or Language object. 
 *
  • Like 1
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...