This repository has been archived on 2025-09-29. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
drupalcampbristol/web/modules/contrib/webform/src/Element/WebformEmailMultiple.php
2017-03-16 15:29:07 +00:00

83 lines
2.5 KiB
PHP

<?php
namespace Drupal\webform\Element;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a webform element for entering multiple comma delimited email addresses.
*
* @FormElement("webform_email_multiple")
*/
class WebformEmailMultiple extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => 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;
}
}