2

I created a Custom Webform Handler, which uses the email and name data from the form input to subscribe the user to a newsletter service through the vendors API.

Ich created a configuration form with this handler, where I can input the API key and the mailing list of the newsletter service.

This all works well up until the point where I want to translate the webform to a different language. I need to have a different configuration for the mailing list in German than in English.

I checked the code of the handlers that come with the webform module, but I can't figure out, how to define, which configuration field values should be translated. I need to have the newsletter_list form element to be translated.

Can anyone help?

This is my shortened code

<?php
/**
  * @file custom_webform_handler/src/Plugin/WebformHandler/NewsletterWebformHandler.php
  */
namespace Drupal\custom_webform_handler\Plugin\WebformHandler;

use Drupal\webform\Plugin\WebformHandlerBase;

// ...
/**
 * ´Newsletter Webform handler.
 *
 * @WebformHandler(
 *   id = "newsletter",
 *   label = @Translation("Newsletter Subscription"),
 *   category = @Translation("Subscription"),
 *   description = @Translation("Subscribe to Newsletter"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_IGNORED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */

class NewsletterWebformHandler extends WebformHandlerBase
{
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'newsletter_apikey' => '',
      'newsletter_list' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['newsletter'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Newsletter settings'),
    ];
    $form['newsletter']['newsletter_apikey'] = [
      '#type' => 'textfield',
      '#title' => $this->t('API key'),
      '#default_value' => $this->configuration['newsletter_apikey'],
      '#required' => TRUE,
    ];
    $form['newsletter']['newsletter_list'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Mailing List'),
      '#default_value' => $this->configuration['newsletter_list'],
      '#required' => TRUE,
    ];
    
    return $this->setSettingsParents($form);

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

// ...
}

I read that I need to have a schema for doing this, so I tried adding one, but it didn't change anything

# file custom_webform_handler/config/schema/custom_webform_handler.schema.yml
webform.handler.newsletter:
  type: mapping
  label: 'Newsletter'
  mapping:
    newsletter_list:
      label: 'Mailing List'
      type: string


Edit

To make it more clear: I created a Handler that has this configuration form

enter image description here

The connection settings are the ones that need to be translated

When I go to the translation page of my webform, I can translate the Email-Handler settings, but not my custom Newsletter Subscription handler settings

enter image description here

1
2

I did provide the wrong type. It should be label and not string

webform.handler.newsletter2go:
  type: mapping
  label: 'Newsletter2go'
  mapping:
    newsletter2go_formcode:
      label: Formcode
      type: label
2
0
+50

You should be able to add translations in /config/install/language/[langcode]/config.name.yml

so /config/install/language/fr/custom_webform_handler.handler.newsletter.yml

But the whole config translation of not really well supported/buggy, you need to make sure you're default language is EN! If not, cli_language may help, cli language needs to be set to EN, and your module needs to be installed via cli (fe. drush).


Update: To be able to translate your custom configuration you need to provide something, like a custom_webform.config_translation.yml:

custom_webform.newsletter_settings_form: base_route_name: [not sure about the route ..] names: - webform.handler.newsletter

Dunno about which route to choose ... maybe entity.webform.settings_form

See also: https://www.drupal.org/docs/user_guide/en/language-config-translate.html

3
  • I don't know, if you understood the issue correctly. I don't want to add translations in the module itself. I want to enable the user to translate the value of the configuration fields. – yunzen Nov 5 '20 at 14:04
  • I added two screenshot for clarification – yunzen Nov 5 '20 at 14:15
  • See also here for the config_translation.yml - drupal.org/docs/user_guide/en/language-config-translate.html – rémy Nov 5 '20 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.