You are here

function mobile_number_element_process in Mobile Number 7

Mobile number element process callback.

1 string reference to 'mobile_number_element_process'
mobile_number_element_info in ./mobile_number.module
Implements hook_element_info().

File

./mobile_number.module, line 732
mobile_number.module

Code

function mobile_number_element_process($element, &$form_state) {
  global $user;
  $element['#tree'] = TRUE;
  $field_name = $element['#name'];
  $field_path = implode('][', $element['#parents']);
  $id = $element['#id'];
  $element += array(
    '#default_value' => array(),
    '#mobile_number' => array(),
  );
  $element['#mobile_number'] += array(
    'allowed_countries' => array(),
    'verify' => mobile_number_sms_sending_is_enabled() ? MOBILE_NUMBER_VERIFY_OPTIONAL : MOBILE_NUMBER_VERIFY_NONE,
    'message' => MOBILE_NUMBER_DEFAULT_SMS_MESSAGE,
    'tfa' => FALSE,
    'token_data' => array(),
    'placeholder' => NULL,
  );
  $settings = $element['#mobile_number'];
  $element['#default_value'] += array(
    'value' => '',
    'country' => count($settings['allowed_countries']) == 1 ? key($settings['allowed_countries']) : 'US',
    'local_number' => '',
    'verified' => 0,
    'tfa' => 0,
  );
  try {
    $default_mobile_number = new MobileNumber($element['#default_value']['value']);
    $element['#default_value']['country'] = $default_mobile_number->country;
  } catch (Exception $e) {
  }
  $value = $element['#value'];
  $element['#prefix'] = "<div class=\"mobile-number-field form-item {$field_name}\" id=\"{$id}\">" . theme_form_element_label(array(
    'element' => $element,
  ));
  $element['#suffix'] = '</div>';
  $mobile_number = NULL;
  $verified = FALSE;
  $countries = MobileNumber::getCountryOptions($settings['allowed_countries'], TRUE);
  $countries += MobileNumber::getCountryOptions(array(
    $element['#default_value']['country'] => $element['#default_value']['country'],
  ), TRUE);
  $default_country = $element['#default_value']['country'];
  if (!empty($value['value'])) {
    try {
      $mobile_number = new MobileNumber($value['value'], NULL);
      $verified = $mobile_number
        ->isVerified();
      $default_country = $mobile_number->country;
      $countries += MobileNumber::getCountryOptions(array(
        $mobile_number->country => $mobile_number->country,
      ));
    } catch (Exception $e) {
    }
  }
  $verified = $verified || !empty($element['#default_value']['verified']) && !empty($value['value']) && $value['value'] == $element['#default_value']['value'];
  $element['country-code'] = array(
    '#type' => 'select',
    '#options' => $countries,
    '#default_value' => $default_country,
    '#access' => !(count($countries) == 1),
    '#attributes' => array(
      'class' => array(
        'country',
      ),
    ),
    '#title' => t('Country Code'),
    '#title_display' => 'invisible',
  );
  $element['mobile'] = array(
    '#type' => 'textfield',
    '#default_value' => $mobile_number ? $mobile_number->libUtil
      ->format($mobile_number->libPhoneNumber, 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' => array(
      'class' => array(
        'local-number',
      ),
      'placeholder' => isset($settings['placeholder']) ? $settings['placeholder'] ? t($settings['placeholder']) : '' : t('Phone number'),
    ),
  );
  $element['mobile']['#attached']['css'][] = drupal_get_path('module', 'mobile_number') . '/css/mobile-number-form-element.css';
  $element['mobile']['#attached']['css'][] = drupal_get_path('module', 'mobile_number') . '/css/mobile-number-flags.css';
  $element['mobile']['#attached']['js'][] = drupal_get_path('module', 'mobile_number') . '/js/mobile-number-form-element.js';
  if ($settings['verify'] != MOBILE_NUMBER_VERIFY_NONE) {
    $element['send_verification'] = array(
      '#type' => 'button',
      '#value' => t('Send verification code'),
      '#ajax' => array(
        'callback' => 'mobile_number_verify_ajax',
        'wrapper' => $id,
        'effect' => 'fade',
      ),
      '#name' => implode('__', $element['#parents']) . '__send_verification',
      '#mobile_number_op' => 'mobile_number_send_verification',
      '#attributes' => array(
        'class' => array(
          !$verified ? 'show' : '',
          'send-button',
        ),
      ),
    );
    $element['verification_code'] = array(
      '#type' => 'textfield',
      '#title' => t('Verification Code'),
      '#prefix' => '<div class="verification"><div class="description">' . t('A verification code has been sent to your mobile. Enter it here.') . '</div>',
    );
    $element['verification_token'] = array(
      '#type' => 'hidden',
      '#value' => !empty($form_state['storage']['mobile_number_fields'][$field_path]['token']) ? $form_state['storage']['mobile_number_fields'][$field_path]['token'] : '',
      '#name' => $field_name . '[verification_token]',
    );
    $element['verify'] = array(
      '#type' => 'button',
      '#value' => t('Verfiy'),
      '#ajax' => array(
        'callback' => 'mobile_number_verify_ajax',
        'wrapper' => $id,
        'effect' => 'fade',
      ),
      '#suffix' => '</div>',
      '#name' => implode('__', $element['#parents']) . '__verify',
      '#mobile_number_op' => 'mobile_number_verify',
      '#attributes' => array(
        'class' => array(
          'verify-button',
        ),
      ),
    );
    if (!empty($settings['tfa'])) {
      $element['tfa'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable two-factor authentication'),
        '#default_value' => !empty($value['tfa']) ? 1 : 0,
        '#prefix' => '<div class="mobile-number-tfa">',
        '#suffix' => '</div>',
      );
    }
  }
  if (!empty($element['#description'])) {
    $element['description']['#markup'] = '<div class="description">' . $element['#description'] . '</div>';
  }
  return $element;
}