You are here

public function MobileNumberInternationalFormatter::viewElements in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/MobileNumberInternationalFormatter.php \Drupal\mobile_number\Plugin\Field\FieldFormatter\MobileNumberInternationalFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/MobileNumberInternationalFormatter.php, line 68

Class

MobileNumberInternationalFormatter
Plugin implementation of the 'mobile_number_international' formatter.

Namespace

Drupal\mobile_number\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  /** @var \Drupal\mobile_number\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');
  $element = [];
  $settings = $this
    ->getSettings() + static::defaultSettings();
  foreach ($items as $delta => $item) {

    /** @var \Drupal\mobile_number\Plugin\Field\FieldType\MobileNumberItem $item */
    if ($mobile_number = $util
      ->getMobileNumber($item
      ->getValue()['value'], NULL, [])) {
      if (!empty($settings['as_link'])) {
        $element[$delta] = [
          '#type' => 'link',
          '#title' => $util
            ->libUtil()
            ->format($mobile_number, $this->phoneDisplayFormat),
          '#url' => Url::fromUri("tel:" . $util
            ->getCallableNumber($mobile_number)),
        ];
      }
      else {
        $element[$delta] = [
          '#plain_text' => $util
            ->libUtil()
            ->format($mobile_number, $this->phoneDisplayFormat),
        ];
      }
    }
  }
  return $element;
}