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 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?
       
    • By Martin Muzatko
      Hello!
      I'm trying to use the data I create in Processwire as much as possible.
      So for a form, I try to use the fields description, name and also its built-in validation rules I defined in ProcessWire on the front-end. (minlength, ranges, patterns, etc)
      I already looked into this tutorial, but it is using external resources to validate the form.
      Since ProcessWire does all the heavy lifting, when processing data, I don't have to sanitize anything - ___processInput should do the job just fine.
      However, it is not actually working correctly. 
      $fields = $templates->get('user')->fields; $submission = $input->post; foreach ($submission as $key => $value) { $field = $fields->{$key}; if ($field instanceof Field) { $field = $field->getInputfield($user); $field->___processInput(new WireInputData([$key => $value])); var_dump($field->getErrors(true)); // retrieve validation error } } This works for some constraints, but the values are not correctly validated.
      Example: 

      All the fields are required and zip is an integer field.
      Yet, I get no validation error for zip, although it was entered as a string, and not an integer. Funny enough: if I provide a number outside the range, I get "Specified value 2 removed because it is out of bounds (min=1000, max=99999)".
      firstname will not return any error for being a required field.
      From what I have looked through the source code, there is no check for "required". Some fields only validate on setAttribute. Am I missing anything or am I doomed to build my own validation process?
      Thank you in advance!
      Best,
      Martin
    • By Marcel Stäheli
      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?