You are here

function telephone_validation_form_field_config_edit_form_alter in Telephone Validation 8.2

Implements hook_form_FORM_ID_alter().

File

./telephone_validation.module, line 37
Validate telephone number.

Code

function telephone_validation_form_field_config_edit_form_alter(&$form, FormStateInterface &$form_state) {

  /** @var \Drupal\field\Entity\FieldConfig $field */
  $field = $form_state
    ->getFormObject()
    ->getEntity();

  // Check field type and add config form but only if field type is telephone.
  if ($field
    ->getType() == 'telephone') {

    /** @var \Drupal\telephone_validation\Validator $validator */
    $validator = \Drupal::service('telephone_validation.validator');
    $form['telephone_validation'] = [
      '#type' => 'fieldset',
      '#title' => t('Telephone validation'),
    ];

    // Let people decide if they want to have validation on or off per instance.
    $settings = $field
      ->getThirdPartySettings('telephone_validation');
    $form['telephone_validation']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Enabled'),
      '#default_value' => !empty($settings),
    ];

    // Choose which telephone format is valid for this field.
    $form['telephone_validation']['format'] = [
      '#type' => 'select',
      '#title' => t('Format'),
      '#description' => t('Validation format. It is recommended to use E164 validation format. Validator will automatically discover country of origin. If you want to limit field instance to only one country you can change it National format and choose country in field below.'),
      '#default_value' => $field
        ->getThirdPartySetting('telephone_validation', 'format', PhoneNumberFormat::E164),
      '#options' => [
        PhoneNumberFormat::E164 => t('E164'),
        PhoneNumberFormat::NATIONAL => t('National'),
      ],
      '#ajax' => [
        'callback' => '_telephone_validation_country_ajax_callback',
        'wrapper' => 'telephone-validation-country',
        'method' => 'replace',
      ],
      '#states' => [
        'invisible' => [
          ':input[name="enabled"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];

    // Choose valid country (or many countries).
    $val = $form_state
      ->getValue('format') ?: $form['telephone_validation']['format']['#default_value'];
    $form['telephone_validation']['country'] = [
      '#type' => 'select',
      '#title' => t('Country'),
      '#description' => t('If no country selected all countries are valid.'),
      '#default_value' => $field
        ->getThirdPartySetting('telephone_validation', 'country'),
      '#multiple' => $val != PhoneNumberFormat::NATIONAL,
      '#options' => $validator
        ->getCountryList(),
      '#prefix' => '<div id="telephone-validation-country">',
      '#suffix' => '</div>',
      '#states' => [
        'invisible' => [
          ':input[name="enabled"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];

    // Add submit handler.
    $form['actions']['submit']['#submit'][] = 'telephone_validation_form_field_config_edit_form_submit';
  }
}