You are here

public static function PhoneInternationalElement::preRenderInternationalPhone in International Phone 8

Same name and namespace in other branches
  1. 8.2 src/Element/PhoneInternationalElement.php \Drupal\phone_international\Element\PhoneInternationalElement::preRenderInternationalPhone()

File

src/Element/PhoneInternationalElement.php, line 59

Class

PhoneInternationalElement
Provides a phone_international form.

Namespace

Drupal\phone_international\Element

Code

public static function preRenderInternationalPhone($element) {
  $element['#attributes']['type'] = 'tel';
  $element['#attributes']['class'][] = 'phone_international-number';
  $element['#attached']['library'][] = 'phone_international/phone_international';

  // Add auto geolocation.
  if (!array_key_exists('data-geo', $element['#attributes'])) {
    $element['#attributes']['data-geo'] = 1;
  }

  // Add default country.
  if (!array_key_exists('data-country', $element['#attributes'])) {
    $element['#attributes']['data-country'] = 'PT';
  }

  // Get Module path to load utilsTellInput.js.
  $module_handler = Drupal::service('module_handler');
  $module_path = $module_handler
    ->getModule('phone_international')
    ->getPath();
  $element['#attached']['drupalSettings']['phone_international']['path'] = $module_path;
  Element::setAttributes($element, [
    'id',
    'name',
    'value',
    'size',
    'maxlength',
    'placeholder',
    'disabled',
    'pattern',
  ]);
  static::setAttributes($element, [
    'form-tel',
  ]);
  return $element;
}