You are here

public function GeolocationZoomGoogleMapFormatter::viewElements in Geolocation Address Link 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 GeolocationGoogleMapFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/GeolocationZoomGoogleMapFormatter.php, line 30

Class

GeolocationZoomGoogleMapFormatter
Plugin implementation of the 'geolocation_zoom_map' formatter.

Namespace

Drupal\geolocation_address_link\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  if (empty($elements)) {
    return $elements;
  }

  // See if there is a zoom value stored in the field's data. If so, swap it into
  // the map settings.
  if (array_key_exists('#attached', $elements)) {
    $data = $items
      ->get(0)
      ->getValue()['data'];
    if (array_key_exists('zoom', $data)) {
      $unique_id = $elements['#uniqueid'];
      $elements['#attached']['drupalSettings']['geolocation']['maps'][$unique_id]['settings']['google_map_settings']['zoom'] = $data['zoom'];
    }
  }
  else {
    foreach ($elements as $delta => $element) {
      $data = $items
        ->get($delta)
        ->getValue()['data'];
      if (array_key_exists('zoom', $data)) {
        $unique_id = $element['#uniqueid'];
        $elements[$delta]['#attached']['drupalSettings']['geolocation']['maps'][$unique_id]['settings']['google_map_settings']['zoom'] = $data['zoom'];
      }
    }
  }
  return $elements;
}