You are here

public function Leaflet::alterRenderArray in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 modules/geolocation_leaflet/src/Plugin/geolocation/MapProvider/Leaflet.php \Drupal\geolocation_leaflet\Plugin\geolocation\MapProvider\Leaflet::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_leaflet/src/Plugin/geolocation/MapProvider/Leaflet.php, line 479

Class

Leaflet
Provides Leaflet maps.

Namespace

Drupal\geolocation_leaflet\Plugin\geolocation\MapProvider

Code

public function alterRenderArray(array $render_array, array $map_settings, array $context = []) {
  if (!empty($map_settings['maxBounds']) && !empty($map_settings['maxBounds']['north_east_bound']) && !empty($map_settings['maxBounds']['south_west_bound']) && isset($map_settings['maxBounds']['north_east_bound']['lat']) && isset($map_settings['maxBounds']['north_east_bound']['lng']) && isset($map_settings['maxBounds']['south_west_bound']['lat']) && isset($map_settings['maxBounds']['south_west_bound']['lng']) && $map_settings['maxBounds']['north_east_bound']['lat'] != '' && $map_settings['maxBounds']['north_east_bound']['lng'] != '' && $map_settings['maxBounds']['south_west_bound']['lat'] != '' && $map_settings['maxBounds']['south_west_bound']['lng'] != '') {
    $map_settings['maxBounds'] = [
      [
        (double) $map_settings['maxBounds']['north_east_bound']['lat'],
        (double) $map_settings['maxBounds']['north_east_bound']['lng'],
      ],
      [
        (double) $map_settings['maxBounds']['south_west_bound']['lat'],
        (double) $map_settings['maxBounds']['south_west_bound']['lng'],
      ],
    ];
  }
  else {
    unset($map_settings['maxBounds']);
  }
  $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
    'library' => [
      'geolocation_leaflet/geolocation.leaflet',
    ],
    'drupalSettings' => [
      'geolocation' => [
        'maps' => [
          $render_array['#id'] => [
            'settings' => [
              'leaflet_settings' => $map_settings,
            ],
          ],
        ],
      ],
    ],
  ]);
  return parent::alterRenderArray($render_array, $map_settings, $context);
}