gebeer

Processing fields for multi language forms

5 posts in this topic

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

3 people like this

Share this post


Link to post
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);
        }
1 person likes this

Share this post


Link to post
Share on other sites

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. 
 *
1 person likes this

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By muco
      Hi guys,
      let's assume I'm developing a site (custom theme with custom fields) for my customer, where he can list any articles. For example: Article1 -> item1, item2, item3, item4, etc... Each item is a textfield. 
      Now if I want to add a new item, in this example item5, then I have to clone an existing field in the admin panel and also add this field to pagefile with PHP. If there would be 100 items, do I really have to clone an existing field and add this field to PHP code everytime? 
      But my main question is: if my customer want's to add a new item (on the admin panel with own user), is there a way that he can clone an existing field (item) and edit the text inside and publish this field on the site, without customizing with PHP?
    • By holmescreek
      I've been trying to figure a way to output the values of several fields within a single repeater using a foreach() without having to reference the name of the fields.
      Typically, for repeaters, I understand you would do foreach($repeater_field as $afield) { echo $afield->fieldname_a; } pretty straight forward.
      Anyways, what I'm doing is using a single repeater as just a container for other fields.
      I've tried using $page->my_repeater->getArray(), then looping through the array, and all sorts of methods, without luck. It seems I must specify the field name for output.
      For instance, my (single) repeater is set up to hold a few different fields:
      my_repeater
      field_a field_b field_c Trying to output it using something like this:
      foreach($page->my_repeater as $afield) {      echo $afield->value;   // I'm not wanting to have the reference the field name e.g. use $afield->field_a }  
      Any suggestions?
       
    • By bmacnaughton
      I would like to be able to fetch the labels for fields in a language different than the current logged in user.
      For field values that's easy 
      // p is page, l is language, and f is field $p->getLanguageValue($l, $f); I'm looking for something like
      $fields->getLanguageValue('en', 'length'); The only solution I know of is to save the current user language, iterate through the languages by setting the user language and fetching the value, and then restore the user language.
       
    • By hellomoto
      I have a fieldsettab I want to populate with two collapsable fields that are for display not input. One will display data directly from an extra MySQL table (corresponding with PW pages) and the other will list certain PW pages. How do I go about this? I wanted to lay them out using Dynatable, one per each field. Thanks.
    • By neonwired
      I have a category template and each page can be associated with multiple categories using a page field type.
      i have this in php : - 
      $categories = $pages->find("template=category, limit=6"); I want to sort the results by the amount of pages each category has, is there a way to do this in a selector or is there a better way of doing this?