You are here

public function GeofieldGoogleMapFormatter::viewElements in Geofield Map 8.2

Same name and namespace in other branches
  1. 8 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 691

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_type}_{$bundle}_{$entity_id}_{$field->getName()}"),
    'map_settings' => $map_settings,
    'data' => [],
  ];

  // Get and set the Geofield cardinality.
  $js_settings['map_settings']['geofield_cardinality'] = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->getCardinality();

  // Get token context.
  $token_context = [
    'field' => $items,
    $this->fieldDefinition
      ->getTargetEntityTypeId() => $items
      ->getEntity(),
  ];
  $description = [];
  $description_field = isset($map_settings['map_marker_and_infowindow']['infowindow_field']) ? $map_settings['map_marker_and_infowindow']['infowindow_field'] : NULL;

  /* @var \Drupal\Core\Field\FieldItemList $description_field_entity */
  $description_field_entity = $entity->{$description_field};

  // 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)) {
    if ($map_settings['map_marker_and_infowindow']['infowindow_field'] === 'title') {
      $description[] = $entity
        ->label();
    }
    elseif (isset($entity->{$description_field})) {
      $description_field_cardinality = $description_field_entity
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getCardinality();
      foreach ($description_field_entity
        ->getValue() as $value) {
        $description[] = isset($value['value']) ? $value['value'] : '';
        if ($description_field_cardinality == 1 || $map_settings['map_marker_and_infowindow']['multivalue_split'] == FALSE) {
          break;
        }
      }
    }
  }

  // Define a Tooltip for the Feature.
  $tooltip = isset($map_settings['map_marker_and_infowindow']['tooltip_field']) && $map_settings['map_marker_and_infowindow']['tooltip_field'] == 'title' ? $entity
    ->label() : '';
  $geojson_data = $this
    ->getGeoJsonData($items, $entity
    ->id(), $description, $tooltip);

  // Add Custom Icon, if set.
  if (isset($map_settings['map_marker_and_infowindow']['icon_image_mode']) && $map_settings['map_marker_and_infowindow']['icon_image_mode'] === 'icon_file') {
    $image_style = 'none';
    $fid = NULL;
    if (isset($map_settings['map_marker_and_infowindow']['icon_file_wrapper']['image_style'])) {
      $image_style = $map_settings['map_marker_and_infowindow']['icon_file_wrapper']['image_style'];
    }
    if ((int) (!empty($map_settings['map_marker_and_infowindow']['icon_file_wrapper']['icon_file']['fids']))) {
      $fid = $map_settings['map_marker_and_infowindow']['icon_file_wrapper']['icon_file']['fids'];
    }
    foreach ($geojson_data as $k => $datum) {
      if ($datum['geometry']->type === 'Point') {
        $geojson_data[$k]['properties']['icon'] = $this->markerIcon
          ->getFileManagedUrl($fid, $image_style);

        // Flag the data with theming, for later rendering logic.
        $geojson_data[$k]['properties']['theming'] = TRUE;
      }
    }
  }
  elseif (isset($map_settings['map_marker_and_infowindow']['icon_image_mode']) && $map_settings['map_marker_and_infowindow']['icon_image_mode'] === 'icon_image_path') {
    foreach ($geojson_data as $k => $datum) {
      if ($datum['geometry']->type === 'Point') {
        $geojson_data[$k]['properties']['icon'] = !empty($map_settings['map_marker_and_infowindow']['icon_image_path']) ? $this->token
          ->replace($map_settings['map_marker_and_infowindow']['icon_image_path'], $token_context) : '';

        // Flag the data with theming, for later rendering logic.
        $geojson_data[$k]['properties']['theming'] = TRUE;
      }
    }
  }

  // Associate dynamic path properties (token based) to the feature,
  // in case of not point.
  foreach ($geojson_data as $k => $datum) {
    if ($datum['geometry']->type !== 'Point') {
      $geojson_data[$k]['properties']['path_options'] = !empty($map_settings['map_geometries_options']) ? str_replace([
        "\n",
        "\r",
      ], "", $this->token
        ->replace($map_settings['map_geometries_options'], $token_context)) : '';
    }
  }
  if (empty($geojson_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' => $geojson_data,
    ];
  }

  // Allow other modules to add/alter the map js settings.
  $this->moduleHandler
    ->alter('geofield_map_googlemap_formatter', $js_settings, $items);
  $element = [
    geofield_map_googlemap_render($js_settings),
  ];

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