You are here

public function MobileNumberCountryFormatter::viewElements in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldFormatter/MobileNumberCountryFormatter.php \Drupal\mobile_number\Plugin\Field\FieldFormatter\MobileNumberCountryFormatter::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/MobileNumberCountryFormatter.php, line 68

Class

MobileNumberCountryFormatter
Plugin implementation of the 'mobile_number_country' 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 ($settings['type'] == 'code') {
        $element[$delta] = [
          '#plain_text' => $util
            ->getCountry($mobile_number),
        ];
      }
      else {
        $element[$delta] = [
          '#plain_text' => $util
            ->getCountryName($util
            ->getCountry($mobile_number)),
        ];
      }
    }
  }
  return $element;
}