You are here

public function TelephoneFormatter::viewElements in Telephone Formatter 8

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/TelephoneFormatter.php, line 150

Class

TelephoneFormatter
Plugin implementation of the 'telephone_formatter' formatter.

Namespace

Drupal\telephone_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  foreach ($items as $delta => $item) {
    try {
      if ($this
        ->getSetting('link')) {
        $element[$delta] = $this
          ->viewLinkValue($item);
      }
      else {
        $element[$delta] = $this
          ->viewFormattedValue($item);
      }
    } catch (\Exception $e) {
      $element[$delta] = $this
        ->viewPlainValue($item);
    }
  }
  return $element;
}