You are here

public static function WebformEmailConfirm::processWebformEmailConfirm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Element/WebformEmailConfirm.php \Drupal\webform\Element\WebformEmailConfirm::processWebformEmailConfirm()

Expand an email confirm field into two HTML5 email elements.

File

src/Element/WebformEmailConfirm.php, line 61

Class

WebformEmailConfirm
Provides a webform element requiring users to double-element and confirm an email address.

Namespace

Drupal\webform\Element

Code

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