You are here

public function GeolocationTokenFormatter::viewElements in Geolocation Field 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/GeolocationTokenFormatter.php \Drupal\geolocation\Plugin\Field\FieldFormatter\GeolocationTokenFormatter::viewElements()
  2. 8.2 src/Plugin/Field/FieldFormatter/GeolocationTokenFormatter.php \Drupal\geolocation\Plugin\Field\FieldFormatter\GeolocationTokenFormatter::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/GeolocationTokenFormatter.php, line 78

Class

GeolocationTokenFormatter
Plugin implementation of the 'geolocation_token' formatter.

Namespace

Drupal\geolocation\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $token_context = [
    $this->fieldDefinition
      ->getTargetEntityTypeId() => $items
      ->getEntity(),
  ];
  $elements = [];
  foreach ($items as $delta => $item) {
    $token_context['geolocation_current_item'] = $item;
    $tokenized_text = \Drupal::token()
      ->replace($this
      ->getSetting('tokenized_text'), $token_context, [
      'callback' => [
        $this,
        'geolocationItemTokens',
      ],
      'clear' => TRUE,
    ]);
    $elements[$delta] = [
      '#markup' => $tokenized_text,
    ];
  }
  return $elements;
}