You are here

public static function MobileNumber::valueCallback in Mobile Number 8

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

Mobile number element value callback.

Parameters

array $element: Element.

bool $input: Input.

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

Return value

array Value.

Overrides FormElement::valueCallback

File

src/Element/MobileNumber.php, line 68

Class

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

Namespace

Drupal\mobile_number\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {

  /** @var \Drupal\mobile_number\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');
  $result = [];
  if ($input) {
    $settings = !empty($element['#mobile_number']) ? $element['#mobile_number'] : [];
    $settings += [
      'allowed_countries' => [],
    ];
    $country = !empty($input['country-code']) ? $input['country-code'] : (count($settings['allowed_countries']) == 1 ? key($settings['allowed_countries']) : NULL);
    $mobile_number = $util
      ->getMobileNumber($input['mobile'], $country);
    $result = [
      'value' => $mobile_number ? $util
        ->getCallableNumber($mobile_number) : '',
      'country' => $country,
      'local_number' => $input['mobile'],
      'tfa' => !empty($input['tfa']) ? 1 : 0,
      'verified' => 0,
      'verification_token' => !empty($input['verification_token']) ? $input['verification_token'] : NULL,
      'verification_code' => !empty($input['verification_code']) ? $input['verification_code'] : NULL,
    ];
  }
  else {
    $result = !empty($element['#default_value']) ? $element['#default_value'] : [];
  }
  return $result;
}