webform_multiple element is not translatable

Created on 31 March 2024, 3 months ago
Updated 24 April 2024, 2 months ago

Problem/Motivation

I am using the webform_multiple element in a custom WebformElement that is a composite element. When I go to the translation page in to translate the form, only the element title is visible to translate. I've stepped through all the code which led me to the Drupal\webform\WebformTranslationConfigManager::buildConfigWebformFormDefaultPropertyElement() method that handles rendering the elements correctly on the add config translation form.

Here is an example of my custom element:

class CaseWizardOptions extends WebformElementBase {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state): array {
    $form = parent::form($form, $form_state);

    $form['solutions'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Solutions'),
      '#open' => TRUE,
    ];

    $form['solutions']['solutions'] = [
      '#type' => 'webform_multiple',
      '#empty_items' => 0,
      '#element' => [
        'action' => [
          '#type' => 'select',
          '#title' => $this->t('Action'),
          '#options' => [
            'recommendation' => $this->t('Show recommendation in modal'),
            'open_case' => $this->t('Open case'),
            'link_account' => $this->t('Link account'),
            'open_new_page' => $this->t('Open new page'),
          ],
        ],
        'title' => [
          '#type' => 'textfield',
          '#title' => $this->t('Title'),
        ],
        'description' => [
          '#type' => 'text_format',
          '#title' => $this->t('Description'),
          '#allowed_formats' => ['full_html'],
        ],
        'recommendation_label' => [
          '#type' => 'textfield',
          '#title' => $this->t('Recommendation label'),
          '#description' => $this->t('Use for recommendation modal action.'),
        ],
        'recommendation' => [
          '#type' => 'text_format',
          '#title' => $this->t('Recommendation'),
          '#description' => $this->t('Use for recommendation modal action.'),
          '#allowed_formats' => ['full_html'],
        ],
        'recommendations_url' => [
          '#type' => 'url',
          '#title' => $this->t('Recommendations url'),
          '#description' => $this->t('Use for recommendation modal action.'),
        ],
        'new_page_url' => [
          '#type' => 'url',
          '#title' => $this->t('New page url'),
          '#description' => $this->t('Use for open new page action.'),
        ],
      ],
      '#weight' => 100,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getOffCanvasWidth(): string {
    return WebformDialogHelper::DIALOG_WIDE;
  }

  /**
   * {@inheritdoc}
   */
  public function prepare(array &$element, ?WebformSubmissionInterface $webform_submission = NULL): void {
    parent::prepare($element, $webform_submission);

    foreach ($element['#solutions'] as $solution) {
      $element['#options'][] = $solution;
    }

    $element['#attached']['library'][] = 'core/drupal.dialog.ajax';
  }

  /**
   * {@inheritdoc}
   */
  protected function defineDefaultProperties(): array {
    $properties = parent::defineDefaultProperties();

    $properties['solutions'] = [];

    // Conditional logic.
    $properties['states'] = [];
    $properties['states_clear'] = TRUE;

    // Wrapper.
    $properties['wrapper_type'] = 'fieldset';

    return $properties;
  }

}

Steps to reproduce

Implement a custom webform element plugin with the example code I've provided above. Attempt to translate the webform and this element will only allow you to translate the element title.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Closed: works as designed

Version

6.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States nathandentzau Rhode Island

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

Comments & Activities

  • Issue created by @nathandentzau
  • Status changed to Postponed: needs info 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    It is unclear what string you are trying to translate. You are correctly using the t() function for your subelements.

    If there is a custom property on the custom webform element that needs to be translated, you would need to implement ::defineTranslatableProperties()

    Please provide more information.

  • πŸ‡ΊπŸ‡ΈUnited States nathandentzau Rhode Island

    J that was it, thank you so much. Adding ::defineTransableProperties() fixed the issue. :)

  • Status changed to Closed: works as designed 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States nathandentzau Rhode Island
Production build 0.69.0 2024