class WebformEmailConfirm in Webform 6.x
Same name in this branch
- 6.x src/Element/WebformEmailConfirm.php \Drupal\webform\Element\WebformEmailConfirm
- 6.x src/Plugin/WebformElement/WebformEmailConfirm.php \Drupal\webform\Plugin\WebformElement\WebformEmailConfirm
Same name and namespace in other branches
- 8.5 src/Element/WebformEmailConfirm.php \Drupal\webform\Element\WebformEmailConfirm
Provides a webform element requiring users to double-element and confirm an email address.
Formats as a pair of email addresses fields, which do not validate unless the two entered email addresses match.
Plugin annotation
@FormElement("webform_email_confirm");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\webform\Element\WebformEmailConfirm uses WebformCompositeFormElementTrait
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WebformEmailConfirm
1 #type use of WebformEmailConfirm
- WebformTestComposite::getCompositeElements in tests/
modules/ webform_test_element/ src/ Element/ WebformTestComposite.php - Get a renderable array of webform elements.
File
- src/
Element/ WebformEmailConfirm.php, line 18
Namespace
Drupal\webform\ElementView source
class WebformEmailConfirm extends FormElement {
use WebformCompositeFormElementTrait;
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#size' => 60,
'#process' => [
[
$class,
'processWebformEmailConfirm',
],
],
'#pre_render' => [
[
$class,
'preRenderWebformCompositeFormElement',
],
],
'#required' => FALSE,
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
if (!isset($element['#default_value'])) {
$element['#default_value'] = '';
}
return [
'mail_1' => $element['#default_value'],
'mail_2' => $element['#default_value'],
];
}
else {
return $input;
}
}
/**
* Expand an email confirm field into two HTML5 email elements.
*/
public static function processWebformEmailConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#tree'] = TRUE;
// Get shared properties.
$shared_properties = [
'#title_display',
'#description_display',
'#size',
'#maxlength',
'#pattern',
'#pattern_error',
'#required',
'#required_error',
'#placeholder',
'#attributes',
];
$element_shared_properties = [
'#type' => 'email',
'#webform_element' => TRUE,
] + array_intersect_key($element, array_combine($shared_properties, $shared_properties));
// Copy wrapper attributes to shared element attributes.
if (isset($element['#wrapper_attributes']) && isset($element['#wrapper_attributes']['class'])) {
foreach ($element['#wrapper_attributes']['class'] as $index => $class) {
if (in_array($class, [
'js-webform-tooltip-element',
'webform-tooltip-element',
])) {
$element_shared_properties['#wrapper_attributes']['class'][] = $class;
unset($element['#wrapper_attributes']['class'][$index]);
}
}
}
// Get mail 1 email element.
$mail_1_properties = [
'#title',
'#description',
'#help_title',
'#help',
'#help_display',
];
$element['mail_1'] = $element_shared_properties + array_intersect_key($element, array_combine($mail_1_properties, $mail_1_properties));
$element['mail_1']['#attributes']['class'][] = 'webform-email';
$element['mail_1']['#value'] = empty($element['#value']) ? NULL : $element['#value']['mail_1'];
$element['mail_1']['#error_no_message'] = TRUE;
// Build mail_2 confirm email element.
$element['mail_2'] = $element_shared_properties;
$element['mail_2']['#title'] = t('Confirm email');
foreach ($element as $key => $value) {
if (strpos($key, '#confirm__') === 0) {
$element['mail_2'][str_replace('#confirm__', '#', $key)] = $value;
}
}
$element['mail_2']['#attributes']['class'][] = 'webform-email-confirm';
$element['mail_2']['#value'] = empty($element['#value']) ? NULL : $element['#value']['mail_2'];
$element['mail_2']['#error_no_message'] = TRUE;
// Initialize the mail elements to allow for webform enhancements.
/** @var \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager */
$element_manager = \Drupal::service('plugin.manager.webform.element');
$element_manager
->buildElement($element['mail_1'], $complete_form, $form_state);
$element_manager
->buildElement($element['mail_2'], $complete_form, $form_state);
// Don't require the main element.
$element['#required'] = FALSE;
// Hide title and description from being display.
$element['#title_display'] = 'invisible';
$element['#description_display'] = 'invisible';
// Remove properties that are being applied to the sub elements.
unset($element['#maxlength'], $element['#attributes'], $element['#description'], $element['#help'], $element['#help_title'], $element['#help_display']);
// Add validate callback.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformEmailConfirm',
]);
// Add flexbox support.
if (!empty($element['#flexbox'])) {
$flex_wrapper = [
'#prefix' => '<div class="webform-flex webform-flex--1"><div class="webform-flex--container">',
'#suffix' => '</div></div>',
];
$element['flexbox'] = [
'#type' => 'webform_flexbox',
'mail_1' => $element['mail_1'] + $flex_wrapper + [
'#parents' => array_merge($element['#parents'], [
'mail_1',
]),
],
'mail_2' => $element['mail_2'] + $flex_wrapper + [
'#parents' => array_merge($element['#parents'], [
'mail_2',
]),
],
];
unset($element['mail_1'], $element['mail_2']);
}
return $element;
}
/**
* Validates an email confirm element.
*/
public static function validateWebformEmailConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
if (isset($element['flexbox'])) {
$mail_element =& $element['flexbox'];
}
else {
$mail_element =& $element;
}
$mail_1 = trim($mail_element['mail_1']['#value']);
$mail_2 = trim($mail_element['mail_2']['#value']);
if (Element::isVisibleElement($element)) {
// Compare email addresses.
if ((!empty($mail_1) || !empty($mail_2)) && strcmp($mail_1, $mail_2)) {
$form_state
->setError($element, t('The specified email addresses do not match.'));
}
else {
// NOTE: Only mail_1 needs to be validated since mail_2 is the same value.
// Verify the required value.
if ($mail_element['mail_1']['#required'] && empty($mail_1)) {
$required_error_title = isset($mail_element['mail_1']['#title']) ? $mail_element['mail_1']['#title'] : NULL;
WebformElementHelper::setRequiredError($element, $form_state, $required_error_title);
}
// Verify that the value is not longer than #maxlength.
if (isset($mail_element['mail_1']['#maxlength']) && mb_strlen($mail_1) > $mail_element['mail_1']['#maxlength']) {
$t_args = [
'@name' => $mail_element['mail_1']['#title'],
'%max' => $mail_element['mail_1']['#maxlength'],
'%length' => mb_strlen($mail_1),
];
$form_state
->setError($element, t('@name cannot be longer than %max characters but is currently %length characters long.', $t_args));
}
}
// Add email validation errors for inline form errors.
// @see \Drupal\Core\Render\Element\Email::validateEmail
$inline_errors = empty($complete_form['#disable_inline_form_errors']) && \Drupal::moduleHandler()
->moduleExists('inline_form_errors');
$mail_error = $form_state
->getError($mail_element['mail_1']);
if ($inline_errors && $mail_error) {
$form_state
->setError($element, $mail_error);
}
}
// Set #title for other validation callbacks.
// @see \Drupal\webform\Plugin\WebformElementBase::validateUnique
if (isset($mail_element['mail_1']['#title'])) {
$element['#title'] = $mail_element['mail_1']['#title'];
}
// Email field must be converted from a two-element array into a single
// string regardless of validation results.
$form_state
->setValueForElement($mail_element['mail_1'], NULL);
$form_state
->setValueForElement($mail_element['mail_2'], NULL);
$element['#value'] = $mail_1;
$form_state
->setValueForElement($element, $mail_1);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformCompositeFormElementTrait:: |
public static | function | Adds form element theming to an element if its title or description is set. | 1 |
WebformEmailConfirm:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
WebformEmailConfirm:: |
public static | function | Expand an email confirm field into two HTML5 email elements. | |
WebformEmailConfirm:: |
public static | function | Validates an email confirm element. | |
WebformEmailConfirm:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |