You are here

public function SettingsForm::buildForm in Telephone Validation 8.2

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/SettingsForm.php, line 77

Class

SettingsForm
Form for default validation settings.

Namespace

Drupal\telephone_validation

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Retrieve configuration object.
  $config = $this
    ->config('telephone_validation.settings');

  // Define valid telephone format.
  $form['format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Format'),
    '#default_value' => $config
      ->get('format') ?: PhoneNumberFormat::E164,
    '#options' => [
      PhoneNumberFormat::E164 => $this
        ->t('E164'),
      PhoneNumberFormat::NATIONAL => $this
        ->t('National'),
    ],
    '#ajax' => [
      'callback' => '::getCountry',
      'wrapper' => 'telephone-validation-country',
      'method' => 'replace',
    ],
  ];

  // Define available countries (or country if format = NATIONAL).
  $val = $form_state
    ->getValue('format') ?: $form['format']['#default_value'];
  $form['country'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Valid countries'),
    '#description' => t('If no country selected all countries are valid.'),
    '#default_value' => $config
      ->get('country') ?: [],
    '#multiple' => $val != PhoneNumberFormat::NATIONAL,
    '#options' => $this->validator
      ->getCountryList(),
    '#prefix' => '<div id="telephone-validation-country">',
    '#suffix' => '</div>',
  ];
  return parent::buildForm($form, $form_state);
}