You are here

public function GeofieldGoogleMapFormatter::viewElements in Geofield Map 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/GeofieldGoogleMapFormatter.php \Drupal\geofield_map\Plugin\Field\FieldFormatter\GeofieldGoogleMapFormatter::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/GeofieldGoogleMapFormatter.php, line 501

Class

GeofieldGoogleMapFormatter
Plugin implementation of the 'geofield_google_map' formatter.

Namespace

Drupal\geofield_map\Plugin\Field\FieldFormatter

Code

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

  // This avoids the infinite loop by stopping the display
  // of any map embedded in an infowindow.
  $view_in_progress =& drupal_static(__FUNCTION__);
  if ($view_in_progress) {
    return [];
  }
  $view_in_progress = TRUE;

  /* @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $items
    ->getEntity();

  // Take the entity translation, if existing.

  /* @var \Drupal\Core\TypedData\TranslatableInterface $entity */
  if ($entity
    ->hasTranslation($langcode)) {
    $entity = $entity
      ->getTranslation($langcode);
  }
  $entity_type = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $entity_id = $entity
    ->id();

  /* @var \Drupal\Core\Field\FieldDefinitionInterface $field */
  $field = $items
    ->getFieldDefinition();
  $map_settings = $this
    ->getSettings();

  // Performs some preprocess on the maps settings before sending to js.
  $this
    ->preProcessMapSettings($map_settings);
  $js_settings = [
    'mapid' => Html::getUniqueId("geofield_map_entity_{$bundle}_{$entity_id}_{$field->getName()}"),
    'map_settings' => $map_settings,
    'data' => [],
  ];
  $description_field = isset($map_settings['map_marker_and_infowindow']['infowindow_field']) ? $map_settings['map_marker_and_infowindow']['infowindow_field'] : NULL;
  $description = [];

  // Render the entity with the selected view mode.
  if (isset($description_field) && $description_field === '#rendered_entity' && is_object($entity)) {
    $build = $this->entityTypeManager
      ->getViewBuilder($entity_type)
      ->view($entity, $map_settings['map_marker_and_infowindow']['view_mode']);
    $description[] = $this->renderer
      ->renderPlain($build);
  }
  elseif (isset($description_field)) {
    $description_field_name = strtolower($map_settings['map_marker_and_infowindow']['infowindow_field']);
    if ($map_settings['map_marker_and_infowindow']['infowindow_field'] === 'title') {
      $description[] = $entity
        ->label();
    }
    elseif (isset($entity->{$description_field_name})) {
      foreach ($entity->{$description_field_name}
        ->getValue() as $value) {
        $description[] = isset($value['value']) ? $value['value'] : '';
        if ($map_settings['map_marker_and_infowindow']['multivalue_split'] == FALSE) {
          break;
        }
      }
    }
  }
  $data = $this
    ->getGeoJsonData($items, $description);
  if (empty($data) && $map_settings['map_empty']['empty_behaviour'] !== '2') {
    $view_in_progress = FALSE;
    return [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $map_settings['map_empty']['empty_behaviour'] === '1' ? $map_settings['map_empty']['empty_message'] : '',
      '#attributes' => [
        'class' => [
          'empty-geofield',
        ],
      ],
    ];
  }
  else {
    $js_settings['data'] = [
      'type' => 'FeatureCollection',
      'features' => $data,
    ];
  }
  $element = [
    geofield_map_googlemap_render($js_settings),
  ];

  // Part of infinite loop stopping strategy.
  $view_in_progress = FALSE;
  return $element;
}