You are here

public function GoogleStaticMaps::alterRenderArray in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 modules/geolocation_google_maps/modules/geolocation_google_static_maps/src/Plugin/geolocation/MapProvider/GoogleStaticMaps.php \Drupal\geolocation_google_static_maps\Plugin\geolocation\MapProvider\GoogleStaticMaps::alterRenderArray()

Alter render array.

Parameters

array $render_array: Render array.

array $map_settings: The current map settings.

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

Return value

array Render attachments.

Overrides MapProviderBase::alterRenderArray

File

modules/geolocation_google_maps/modules/geolocation_google_static_maps/src/Plugin/geolocation/MapProvider/GoogleStaticMaps.php, line 117

Class

GoogleStaticMaps
Provides Google Maps.

Namespace

Drupal\geolocation_google_static_maps\Plugin\geolocation\MapProvider

Code

public function alterRenderArray(array $render_array, array $map_settings, array $context = []) {
  $additional_parameters = [
    'type' => strtolower($map_settings['type']),
    'size' => filter_var($map_settings['width'], FILTER_SANITIZE_NUMBER_INT) . 'x' . filter_var($map_settings['height'], FILTER_SANITIZE_NUMBER_INT),
    'zoom' => $map_settings['zoom'],
    'scale' => (int) $map_settings['scale'],
    'format' => $map_settings['format'],
  ];

  // 0,0 is the default behavior anyway, so just ignore it for fitlocations.
  if (!empty($render_array['#centre']['lat']) || !empty($render_array['#centre']['lng'])) {
    $additional_parameters['center'] = $render_array['#centre']['lat'] . ',' . $render_array['#centre']['lng'];
  }
  $static_map_url = $this
    ->getGoogleMapsApiUrl($additional_parameters);
  $locations = GeolocationMap::getLocations($render_array);
  foreach ($locations as $location) {
    $marker_string = '&markers=';
    if (!empty($location['#icon'])) {
      $marker_string .= 'icon:' . Url::fromRoute('<front>', [], [
        'absolute' => TRUE,
      ])
        ->toString() . $location['#icon'] . urlencode('|');
    }
    $marker_string .= $location['#position']['lat'] . ',' . $location['#position']['lng'];
    $static_map_url .= $marker_string;
  }
  return [
    '#markup' => '<img src="' . $static_map_url . '">',
  ];
}