You are here

public function Telephone::form in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/Telephone.php \Drupal\webform\Plugin\WebformElement\Telephone::form()

Gets the actual configuration webform array to be built.

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 An associative array contain the element's configuration webform without any default values.

Overrides TextBase::form

File

src/Plugin/WebformElement/Telephone.php, line 119

Class

Telephone
Provides a 'tel' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $form['telephone'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Telephone settings'),
  ];
  $form['telephone']['international'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enhance support for international phone numbers'),
    '#description' => $this
      ->t('Enhance the telephone element\'s international support using the jQuery <a href=":href">International Telephone Input</a> plugin.', [
      ':href' => 'http://intl-tel-input.com/',
    ]),
    '#return_value' => TRUE,
  ];
  $form['telephone']['international_initial_country'] = [
    '#title' => $this
      ->t('Initial country'),
    '#type' => 'select',
    '#empty_option' => $this
      ->t('- None -'),
    '#options' => CountryManager::getStandardList(),
    '#states' => [
      'visible' => [
        ':input[name="properties[international]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['telephone']['international_preferred_countries'] = [
    '#title' => $this
      ->t('Preferred countries'),
    '#type' => 'select',
    '#options' => CountryManager::getStandardList(),
    '#description' => $this
      ->t('Specify the countries to appear at the top of the list.'),
    '#select2' => TRUE,
    '#multiple' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="properties[international]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $this->elementManager
    ->processElement($form['telephone']['international_preferred_countries']);
  if ($this->librariesManager
    ->isExcluded('jquery.intl-tel-input')) {
    $form['telephone']['#access'] = FALSE;
    $form['telephone']['international']['#access'] = FALSE;
    $form['telephone']['international_initial_country']['#access'] = FALSE;
    $form['telephone']['international_preferred_countries']['#access'] = FALSE;
  }

  // Add support for telephone_validation.module.
  if (\Drupal::moduleHandler()
    ->moduleExists('telephone_validation')) {
    $form['telephone']['telephone_validation_format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Valid format'),
      '#description' => $this
        ->t('For international telephone numbers we suggest using <a href=":href">E164</a> format.', [
        ':href' => 'https://en.wikipedia.org/wiki/E.164',
      ]),
      '#empty_option' => $this
        ->t('- None -'),
      '#options' => [
        \libphonenumber\PhoneNumberFormat::E164 => $this
          ->t('E164'),
        \libphonenumber\PhoneNumberFormat::NATIONAL => $this
          ->t('National'),
      ],
    ];
    $form['telephone']['telephone_validation_country'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Valid country'),
      '#options' => \Drupal::service('telephone_validation.validator')
        ->getCountryList(),
      '#states' => [
        'visible' => [
          ':input[name="properties[telephone_validation_format]"]' => [
            'value' => \libphonenumber\PhoneNumberFormat::NATIONAL,
          ],
        ],
        'required' => [
          ':input[name="properties[telephone_validation_format]"]' => [
            'value' => \libphonenumber\PhoneNumberFormat::NATIONAL,
          ],
        ],
      ],
    ];
    $form['telephone']['telephone_validation_countries'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Valid countries'),
      '#description' => $this
        ->t('If no country selected all countries are valid.'),
      '#options' => \Drupal::service('telephone_validation.validator')
        ->getCountryList(),
      '#select2' => TRUE,
      '#multiple' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="properties[telephone_validation_format]"]' => [
            'value' => \libphonenumber\PhoneNumberFormat::E164,
          ],
        ],
      ],
    ];
    $this->elementManager
      ->processElement($form['telephone']['telephone_validation_countries']);
  }
  elseif (\Drupal::currentUser()
    ->hasPermission('administer modules')) {
    $t_args = [
      ':href' => 'https://www.drupal.org/project/telephone_validation',
    ];
    $form['telephone']['telephone_validation_message'] = [
      '#type' => 'webform_message',
      '#message_type' => 'info',
      '#message_message' => $this
        ->t('Install the <a href=":href">Telephone validation</a> module which provides international phone number validation.', $t_args),
      '#message_id' => 'webform.telephone_validation_message',
      '#message_close' => TRUE,
      '#message_storage' => WebformMessageElement::STORAGE_STATE,
      '#access' => TRUE,
    ];
  }
  return $form;
}