gebeer

Processing fields for multi language forms

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?

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.

  • Like 3

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);
        }
  • Like 1

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. 
 *
  • Like 1

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 louisstephens
      I have been following @bernhard's great tutorial on creating a Dashboard and everything has very easy to understand so far. However, I need to add a select field that a template is using (so users can create a new page from the dashboard), but I am really at a complete lost on how to achieve this. I have been googling, but can't really find anything on the matter.
      $field = $this->modules->get('InputfieldSelect'); $field->name = "Select Manufacturer"; $field->findPagesSelector = "template=basic-page"; //not needed for select field $field->labelFieldName = "title"; $field->name = "Manufacturer Select"; $field->columnWidth = 34; $fieldset->add($field); I have the above code, which was originally an InputfieldPage, but since this is an options field, I have changed it. Has anyone actually gotten a select from a template (and its' values) in a module?
    • By mwjt42
      How do you include fields in Twig templates?
      Tried using {% $page->Title %}, but that creates an error.
      Thanks
    • By Livius
      Hello fellow Process wire Developers!
      Today i have encountered an issue using the repeater where I can't find out how to solve it therefore i am asking for help.
      To be specific, I am trying to get the "file" field working properly in the repeater but i couldn't find a way on how to assign a property of a field that is being used by the repeater.
      download_pdf is the name of the field that i want to access properties from.
      What i wished i could do was something like $download->download_pdf.size or $download->download_pdf->name, something like that but obviously these didn't work
      Hope there is a solution and thanks in advance!
      Best wishes,
      Livius

    • By quickjeff
      Hi Guys, 
      I recently have received a client who has various page layouts. I wanted to give them the ability to simply add their HTML into a body field with CKEditor to allow HTML customization.
      However, the client isn't very familiar with HTML.
      This has left me to build a template that has multiple of the similar field types.
      The thing is, I cannot use the repeater since I cannot repeat lets say an image for repeated item 1 and only body field for repeater item 2.
      Therefore, I have to literally add 15 fields for them to totally be able to edit the page without issues.
      What are your thoughts on this approach?
      Also, I have to name the template according to what it contains, so I was thinking of using numbers instead of words.
      Reason being, numbers can describe the amount of title fields, body fields and images.
      Therefore something like, 3-3-2.php or three-three-two.php 
      I have never run into a situation where the client needed so many fields. I was always able to get around it with repeaters etc. 
      Thoughts?
    • By Kiwi Chris
      Forms are an essential part of most websites, and it's no surprise that there's an excellent premium module Form Builder but what if you're on a zero budget for whatever reason?
      It is possible to build forms quickly and easily by making use of a couple of free modules and the admin UI to give you a great deal of flexibility and speed of development, particularly if you need multiple forms on a website with different fields.
      1. First you're going to need to install a couple of modules:
      Form Template Processor
      Fieldtype Select External Option
      2. For each form that you want to display, create a template without a template file and add fields to it as you normally would. (eg I have formContact, formRegister etc)
      Tip: under the Advanced tab in the setup for each template, I add a tag Forms so that all my forms templates are nicely grouped together in admin.
      3. Create a new field of type Select External Option and call it formTemplate
      In the section Create options from any database table select
      templates as the source table id as the Option Value name as the Option Label 4. Create a new template file and call it renderForm.php (or whatever else you like)
      Add an email field to this form - This will be the email address that forms get submitted to. Add the formTemplate field you previously created to this form. This will allow you to select which of the templates you previously created such as formContact, formRegister etc you want to render. Add any other fields as usual that you want to render on the page. Add the following PHP code to the template file. $recipient = $page->email; $form = $modules->get('FormTemplateProcessor'); $form->template = $templates->get($page->formTemplate->label); // required $form->requiredFields = array('contactName', 'contactEmail', 'contactMesssage'); //Optional: This can be improved by having a field in the page template with a CSV list of required fields eg $form->requiredFields = explode(',', $page->requiredFields) $form->email = $recipient; // optional, sends form as email. FormTemplateProcessor can also save forms to the database. $content .= $form->render(); //generate the form to display. Note: this doesn't actually render the form at this point, but you have it in the $content variable ready to output wherever you want in your template.
      Add any template HTML or other PHP code and echo $content; wherever you want to render the form.
      5. Create a page using the renderForm template, and provide an email address, and select a form that you want to display.
      6. Use CSS to style the form as required.
      7. View your new page, and check that the form renders correctly.
      8. You can modify the templates you created at step 2 or create new ones as required if your requirements for what fields forms display changes.
      Note: The Form Template Processor module can also save form input as pages, and the FieldType Select External Option can be set up with filtering, so this solution can probably be refined further.