You are here

public function MarkerZoomByAnchor::alterMap in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 modules/geolocation_google_maps/src/Plugin/geolocation/MapFeature/MarkerZoomByAnchor.php \Drupal\geolocation_google_maps\Plugin\geolocation\MapFeature\MarkerZoomByAnchor::alterMap()

Alter render array.

Parameters

array $render_array: Render array.

array $feature_settings: The current feature settings.

array $context: Context like field formatter, field widget or view.

Return value

array Render array.

Overrides MapFeatureFrontendBase::alterMap

File

modules/geolocation_google_maps/src/Plugin/geolocation/MapFeature/MarkerZoomByAnchor.php, line 46

Class

MarkerZoomByAnchor
Provides Google Maps.

Namespace

Drupal\geolocation_google_maps\Plugin\geolocation\MapFeature

Code

public function alterMap(array $render_array, array $feature_settings, array $context = []) {
  if (empty($render_array['#children']['locations'])) {
    return $render_array;
  }
  if (!empty($context['view'])) {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $context['view'];
  }
  foreach ($render_array['#children']['locations'] as &$location) {
    $anchor_id = \Drupal::token()
      ->replace($feature_settings['marker_zoom_anchor_id'], $context);
    if (empty($view)) {
      continue;
    }
    if (empty($location['#attributes'])) {
      $location['#attributes'] = [];
    }
    elseif (!is_array($location['#attributes'])) {
      $location['#attributes'] = new Attribute($location['#attributes']);
      $location['#attributes'] = $location['#attributes']
        ->toArray();
    }
    if (isset($location['#attributes']['data-views-row-index'])) {
      $anchor_id = $view
        ->getStyle()
        ->tokenizeValue($anchor_id, (int) $location['#attributes']['data-views-row-index']);
      $location['#attributes']['data-marker-zoom-anchor-id'] = $anchor_id;
    }
  }
  $render_array = parent::alterMap($render_array, $feature_settings, $context);
  return $render_array;
}