You are here

public function Telephone::prepare in Webform 6.x

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

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides TextBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

File

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

Class

Telephone
Provides a 'tel' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  parent::prepare($element, $webform_submission);

  // Add international library and classes.
  if (!empty($element['#international']) && $this->librariesManager
    ->isIncluded('jquery.intl-tel-input')) {
    $element['#attached']['library'][] = 'webform/webform.element.telephone';
    $element['#attributes']['class'][] = 'js-webform-telephone-international';
    $element['#attributes']['class'][] = 'webform-webform-telephone-international';
    if (!empty($element['#international_initial_country'])) {
      $element['#attributes']['data-webform-telephone-international-initial-country'] = $element['#international_initial_country'];
    }
    if (!empty($element['#international_preferred_countries'])) {
      $element['#attributes']['data-webform-telephone-international-preferred-countries'] = Json::encode($element['#international_preferred_countries']);
    }

    // The utilsScript is fetched when the page has finished loading to
    // prevent blocking.
    // @see https://github.com/jackocnr/intl-tel-input
    $library_path = $this->librariesManager
      ->find('jquery.intl-tel-input');
    $utils_script = $library_path ? '/' . $library_path . '/build/js/utils.js' : FALSE;

    // Load utils.js from CDN defined in webform.libraries.yml.
    if (!$utils_script || !file_exists(DRUPAL_ROOT . $utils_script)) {
      $intl_tel_input_library = $this->libraryDiscovery
        ->getLibraryByName('webform', 'libraries.jquery.intl-tel-input');
      $cdn = reset($intl_tel_input_library['cdn']);
      $utils_script = $cdn . 'build/js/utils.js';
    }
    else {
      $utils_script = base_path() . $library_path . '/build/js/utils.js';
    }
    $element['#attached']['drupalSettings']['webform']['intlTelInput']['utilsScript'] = $utils_script;
  }
  if ($this->moduleHandler
    ->moduleExists('telephone_validation')) {
    $format = $this
      ->getElementProperty($element, 'telephone_validation_format');
    $format = $format !== '' ? (int) $format : '';
    if ($format === \libphonenumber\PhoneNumberFormat::NATIONAL) {
      $country = (array) $this
        ->getElementProperty($element, 'telephone_validation_country');
    }
    else {
      $country = $this
        ->getElementProperty($element, 'telephone_validation_countries');
    }
    if ($format !== '') {
      $element['#element_validate'][] = [
        'Drupal\\telephone_validation\\Render\\Element\\TelephoneValidation',
        'validateTel',
      ];
      $element['#element_validate_settings'] = [
        'format' => $format,
        'country' => $country,
      ];
    }
  }
}