You are here

public static function YamlFormEmailConfirm::processYamlFormEmailConfirm in YAML Form 8

Expand an email confirm field into two HTML5 email elements.

File

src/Element/YamlFormEmailConfirm.php, line 62

Class

YamlFormEmailConfirm
Provides a form element requiring users to double-element and confirm an email address.

Namespace

Drupal\yamlform\Element

Code

public static function processYamlFormEmailConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
  $element['#tree'] = TRUE;

  // Get shared properties.
  $shared_properties = [
    '#title_display',
    '#description_display',
    '#size',
    '#maxlength',
    '#pattern',
    '#required',
    '#placeholder',
    '#attributes',
  ];
  $element_shared_properties = [
    '#type' => 'email',
  ] + array_intersect_key($element, array_combine($shared_properties, $shared_properties));

  // Get mail 1 email element.
  $mail_1_properties = [
    '#title',
    '#description',
  ];
  $element['mail_1'] = $element_shared_properties + array_intersect_key($element, array_combine($mail_1_properties, $mail_1_properties));
  $element['mail_1']['#attributes']['class'][] = 'yamlform-email';
  $element['mail_1']['#value'] = empty($element['#value']) ? NULL : $element['#value']['mail_1'];

  // 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'][] = 'yamlform-email-confirm';
  $element['mail_2']['#value'] = empty($element['#value']) ? NULL : $element['#value']['mail_2'];

  // Remove properties that are being applied to the sub elements.
  $element['#required'] = FALSE;
  unset($element['#title']);
  unset($element['#description']);
  unset($element['#maxlength']);
  unset($element['#atributes']);
  $element['#element_validate'] = [
    [
      get_called_class(),
      'validateYamlFormEmailConfirm',
    ],
  ];
  return $element;
}