TRUE, '#description' => $this->t('Multiple email addresses may be separated by commas.'), '#size' => 60, '#allow_tokens' => FALSE, '#process' => [ [$class, 'processAutocomplete'], [$class, 'processAjaxForm'], [$class, 'processPattern'], ], '#element_validate' => [ [$class, 'validateWebformEmailMultiple'], ], '#pre_render' => [ [$class, 'preRenderWebformEmailMultiple'], ], '#theme' => 'input__email_multiple', '#theme_wrappers' => ['form_element'], ]; } /** * Webform element validation handler for #type 'email_multiple'. */ public static function validateWebformEmailMultiple(&$element, FormStateInterface $form_state, &$complete_form) { $value = trim($element['#value']); $form_state->setValueForElement($element, $value); if ($value) { $values = preg_split('/\s*,\s*/', $value); foreach ($values as $value) { // Allow tokens to be be include in multiple email list. if (!empty($element['#allow_tokens'] && preg_match('/^\[.*\]$/', $value))) { continue; } if (!\Drupal::service('email.validator')->isValid($value)) { $form_state->setError($element, t('The email address %mail is not valid.', ['%mail' => $value])); return; } } } } /** * Prepares a #type 'email_multiple' render element for theme_element(). * * @param array $element * An associative array containing the properties of the element. * Properties used: #title, #value, #description, #size, #maxlength, * #placeholder, #required, #attributes. * * @return array * The $element with prepared variables ready for theme_element(). */ public static function preRenderWebformEmailMultiple(array $element) { $element['#attributes']['type'] = 'text'; Element::setAttributes($element, ['id', 'name', 'value', 'size', 'maxlength', 'placeholder']); static::setAttributes($element, ['form-textfield', 'form-email-multiple']); return $element; } }