You are here

public function InternationalPhoneDefaultFormatter::viewElements in International Phone Field 8

Define how the field type is showed.

Inside this method we can customize how the field is displayed inside pages.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/InternationalPhoneDefaultFormatter.php, line 28

Class

InternationalPhoneDefaultFormatter
Plugin implementation of the 'AddressDefaultFormatter' formatter.

Namespace

Drupal\international_phone\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  foreach ($items as $delta => $item) {
    $text = '';
    if (isset($item->value)) {
      $text = SafeMarkup::checkPlain($item->value);

      // iPhone Support.
      if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) {
        $text = '<a href="tel:' . $text . '">' . $text . '</a>';
      }
    }
    $elements[$delta] = [
      '#type' => 'markup',
      '#markup' => $text,
      '#attached' => [
        'library' => [
          'international_phone/international_phone',
        ],
      ],
    ];
  }
  return $elements;
}