Translating form overwrites English elements

Created on 10 February 2025, 2 months ago

Problem/Motivation

I have 2 forms on my multilingual site both inserted via block. My contact form translated perfectly but the other villa enquiry form didn't.

When I start translating any field in any language (en, it, de) saving the translation overwrites the English elements something like this:

full_name:
'#type': textfield
'#title': 'Nombre'
email_address:
'#type': email
'#title': 'Email address'
telephone:
'#type': textfield
'#title': TelΓ©fono

Which of course breaks the whole form.

Steps to reproduce

Use this source and try to translate to another language:

full_name:
  '#type': textfield
  '#title': 'Full name'
  '#size': 30
  '#required': true
  '#required_error': 'Please enter your full name'
  '#wrapper_attributes':
    class:
      - form-group
email_address:
  '#type': email
  '#title': 'Email address'
  '#size': 30
  '#required': true
  '#wrapper_attributes':
    class:
      - form-group
telephone:
  '#type': textfield
  '#title': Telephone
  '#description': '<p>Please include your country code eg +44</p>'
  '#size': 30
  '#placeholder': '+44'
  '#wrapper_attributes':
    class:
      - form-group
number_of_people:
  '#type': number
  '#title': 'Number of people'
  '#wrapper_attributes':
    class:
      - form-group
  '#attributes':
    class:
      - form-text
  '#min': 1
  '#max': 20
  '#step': 1
arrival_date:
  '#type': date
  '#title': 'Arrival date'
  '#required': true
  '#required_error': 'Please enter your arrival date'
  '#wrapper_attributes':
    class:
      - form-group
  '#date_date_min': today
  '#date_days':
    - '0'
    - '5'
    - '6'
departure_date:
  '#type': date
  '#title': 'Departure date'
  '#required': true
  '#required_error': 'Please enter your departure date'
  '#wrapper_attributes':
    class:
      - form-group
  '#date_date_min': 'today +1 week'
approximate_budget:
  '#type': number
  '#title': 'Approximate budget'
  '#wrapper_attributes':
    class:
      - form-group
comments:
  '#type': textarea
  '#title': Comments
  '#wrapper_attributes':
    class:
      - form-group
captcha:
  '#type': captcha
  '#captcha_type': recaptcha/reCAPTCHA
  '#captcha_admin_mode': true
actions:
  '#type': webform_actions
  '#title': 'Submit button(s)'
  '#submit__label': Send
  '#submit__attributes':
    class:
      - cta-primary

Proposed resolution

Not sure? Are accents causing a problem?

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

6.2

Component

Translation

Created by

πŸ‡ͺπŸ‡ΈSpain elkiwi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @elkiwi
  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦
  • πŸ‡ͺπŸ‡ΈSpain elkiwi

    I've been testing this again to try and track down where the problem lies. I rebuilt the whole form one element at a time, adding the translations one language at a time.

    Then I would preview the form via the "View" tab in the Webform admin.

    This all worked perfectly with all forms displaying with translations.

    But as soon as I view the form on a node where it is embedded as a block, the original (English) form gets overwritten as described in my first post #1

    So I'm not sure now whether there's some other custom code on our site doing this but a I see similar issues still open, I will keep watching both these threads and continue investigating.

  • πŸ‡ͺπŸ‡ΈSpain elkiwi

    I have resolved this issue by using hook_form_alter. Maybe saving the form was a bad idea in the first place when there was no need to:

    function MY_MODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
    {
    if ($node && $node->hasField('field_villa_company')) {
          // Get the webform submission handler settings
          $handlers = $form_state->getFormObject()->getWebform()->getHandlers();
          $email_handler = $handlers->get('email');
    
          if ($email_handler) {
            $settings = $email_handler->getSettings();
    
            // Modify the to_mail based on field_villa_company value
            if ($node->get('field_villa_company')->isEmpty() || $node->get('field_villa_company')->value == '0') {
              $settings['to_mail'] = 'redacted@example.com';
            } elseif ($node->get('field_villa_company')->value == 2) {
              $settings['to_mail'] = 'redacted_2@example.com';
            } else {
              $settings['to_mail'] = 'redacted_3@example.com';
            }
    
            // Update the handler settings
            $email_handler->setSettings($settings);
          }
        }
    
    //...
    

    Now I can safely translate the webform without the English getting overwritten.

Production build 0.71.5 2024