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
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