You are here

public function MobileNumber::mobileNumberProcess in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Element/MobileNumber.php \Drupal\mobile_number\Element\MobileNumber::mobileNumberProcess()

Mobile number element process callback.

Parameters

$element: Element.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

array $complete_form: Complete form.

Return value

array Processed array.

File

src/Element/MobileNumber.php, line 107

Class

MobileNumber
Provides a form input element for entering an email address.

Namespace

Drupal\mobile_number\Element

Code

public function mobileNumberProcess($element, FormStateInterface $form_state, array $complete_form) {

  /** @var \Drupal\mobile_number\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');
  $element['#tree'] = TRUE;
  $field_name = $element['#name'];
  $field_path = implode('][', $element['#parents']);
  $id = $element['#id'];
  $op = $this
    ->getOp($element, $form_state);
  $element += [
    '#default_value' => [],
    '#mobile_number' => [],
  ];
  $errors = $form_state
    ->getErrors();
  foreach ($errors as $path => $message) {
    if (!(strpos($path, $field_path) === 0)) {
      unset($errors[$path]);
    }
  }
  $element['#mobile_number'] += [
    'allowed_countries' => [],
    'verify' => MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_NONE,
    'message' => MobileNumberUtilInterface::MOBILE_NUMBER_DEFAULT_SMS_MESSAGE,
    'tfa' => FALSE,
    'token_data' => [],
    'placeholder' => NULL,
  ];
  $settings = $element['#mobile_number'];
  $element['#default_value'] += [
    'value' => '',
    'country' => count($settings['allowed_countries']) == 1 ? key($settings['allowed_countries']) : 'US',
    'local_number' => '',
    'verified' => 0,
    'tfa' => 0,
  ];
  if ($default_mobile_number = $util
    ->getMobileNumber($element['#default_value']['value'])) {
    $element['#default_value']['country'] = $util
      ->getCountry($default_mobile_number);
  }
  $value = $element['#value'];
  $element['#prefix'] = "<div class=\"mobile-number-field form-item {$field_name}\" id=\"{$id}\">";
  $element['#suffix'] = '</div>';
  $element['label'] = [
    '#type' => 'label',
    '#title' => $element['#title'],
    '#required' => $element['#required'],
    '#title_display' => $element['#title_display'],
  ];
  $mobile_number = NULL;
  $verified = FALSE;
  $countries = $util
    ->getCountryOptions($settings['allowed_countries'], TRUE);
  $countries += $util
    ->getCountryOptions([
    $element['#default_value']['country'] => $element['#default_value']['country'],
  ], TRUE);
  $default_country = $element['#default_value']['country'];
  if (!empty($value['value']) && ($mobile_number = $util
    ->getMobileNumber($value['value']))) {
    $verified = $settings['verify'] != MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_NONE && static::isVerified($element);
    $default_country = $util
      ->getCountry($mobile_number);
    $country = $util
      ->getCountry($mobile_number);
    $countries += $util
      ->getCountryOptions([
      $country => $country,
    ]);
  }
  $element['country-code'] = [
    '#type' => 'select',
    '#options' => $countries,
    '#default_value' => $default_country,
    '#access' => !(count($countries) == 1),
    '#attributes' => [
      'class' => [
        'country',
      ],
    ],
    '#title' => t('Country Code'),
    '#title_display' => 'invisible',
  ];
  $element['mobile'] = [
    '#type' => 'textfield',
    '#default_value' => $mobile_number ? $util
      ->libUtil()
      ->format($mobile_number, 2) : NULL,
    '#title' => t('Phone number'),
    '#title_display' => 'invisible',
    '#suffix' => '<div class="form-item verified ' . ($verified ? 'show' : '') . '" title="' . t('Verified') . '"><span>' . t('Verified') . '</span></div>',
    '#attributes' => [
      'class' => [
        'local-number',
      ],
      'placeholder' => isset($settings['placeholder']) ? $settings['placeholder'] ? t($settings['placeholder']) : '' : t('Phone number'),
    ],
  ];
  $element['mobile']['#attached']['library'][] = 'mobile_number/element';
  if ($settings['verify'] != MobileNumberUtilInterface::MOBILE_NUMBER_VERIFY_NONE) {
    $element['send_verification'] = [
      '#type' => 'button',
      '#value' => t('Send verification code'),
      '#ajax' => [
        'callback' => 'Drupal\\mobile_number\\Element\\MobileNumber::verifyAjax',
        'wrapper' => $id,
        'effect' => 'fade',
      ],
      '#name' => implode('__', $element['#parents']) . '__send_verification',
      '#mobile_number_op' => 'mobile_number_send_verification',
      '#attributes' => [
        'class' => [
          !$verified ? 'show' : '',
          'send-button',
        ],
      ],
      '#submit' => [],
    ];
    $user_input = $form_state
      ->getUserInput();
    $vc_parents = $element['#parents'];
    array_push($vc_parents, 'verification_code');
    if (NestedArray::getValue($user_input, $vc_parents)) {
      NestedArray::setValue($user_input, $vc_parents, '');
      $form_state
        ->setUserInput($user_input);
    }
    $verify_prompt = !$verified && $op && (!$errors || $op == 'mobile_number_verify');
    $element['verification_code'] = [
      '#type' => 'textfield',
      '#title' => t('Verification Code'),
      '#prefix' => '<div class="verification ' . ($verify_prompt ? 'show' : '') . '"><div class="description">' . t('A verification code has been sent to your mobile. Enter it here.') . '</div>',
    ];
    $element['verify'] = [
      '#type' => 'button',
      '#value' => t('Verify'),
      '#ajax' => [
        'callback' => 'Drupal\\mobile_number\\Element\\MobileNumber::verifyAjax',
        'wrapper' => $id,
        'effect' => 'fade',
      ],
      '#suffix' => '</div>',
      '#name' => implode('__', $element['#parents']) . '__verify',
      '#mobile_number_op' => 'mobile_number_verify',
      '#attributes' => [
        'class' => [
          'verify-button',
        ],
      ],
      '#submit' => [],
    ];
    if (!empty($settings['tfa'])) {
      $element['tfa'] = [
        '#type' => 'checkbox',
        '#title' => t('Enable two-factor authentication'),
        '#default_value' => !empty($value['tfa']) ? 1 : 0,
        '#prefix' => '<div class="mobile-number-tfa">',
        '#suffix' => '</div>',
      ];
    }
    $storage = $form_state
      ->getStorage();
    $element['verification_token'] = [
      '#type' => 'hidden',
      '#value' => !empty($storage['mobile_number_fields'][$field_path]['token']) ? $storage['mobile_number_fields'][$field_path]['token'] : '',
      '#name' => $field_name . '[verification_token]',
    ];
  }
  if (!empty($element['#description'])) {
    $element['description']['#markup'] = '<div class="description">' . $element['#description'] . '</div>';
  }
  return $element;
}