You are here

public function LeafletMarkerIcon::alterMap in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 modules/geolocation_leaflet/src/Plugin/geolocation/MapFeature/LeafletMarkerIcon.php \Drupal\geolocation_leaflet\Plugin\geolocation\MapFeature\LeafletMarkerIcon::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 MapFeatureBase::alterMap

File

modules/geolocation_leaflet/src/Plugin/geolocation/MapFeature/LeafletMarkerIcon.php, line 153

Class

LeafletMarkerIcon
Provides marker icon adjustment.

Namespace

Drupal\geolocation_leaflet\Plugin\geolocation\MapFeature

Code

public function alterMap(array $render_array, array $feature_settings, array $context = []) {
  $render_array = parent::alterMap($render_array, $feature_settings, $context);
  $render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
    'library' => [
      'geolocation_leaflet/mapfeature.' . $this
        ->getPluginId(),
    ],
    'drupalSettings' => [
      'geolocation' => [
        'maps' => [
          $render_array['#id'] => [
            $this
              ->getPluginId() => [
              'enable' => TRUE,
              'iconSize' => [
                'width' => (int) $feature_settings['icon_size']['width'],
                'height' => (int) $feature_settings['icon_size']['height'],
              ],
              'iconAnchor' => [
                'x' => (int) $feature_settings['icon_anchor']['x'],
                'y' => (int) $feature_settings['icon_anchor']['y'],
              ],
              'popupAnchor' => [
                'x' => (int) $feature_settings['popup_anchor']['x'],
                'y' => (int) $feature_settings['popup_anchor']['y'],
              ],
              'shadowSize' => [
                'width' => (int) $feature_settings['shadow_size']['width'],
                'height' => (int) $feature_settings['shadow_size']['height'],
              ],
              'shadowAnchor' => [
                'x' => (int) $feature_settings['shadow_anchor']['x'],
                'y' => (int) $feature_settings['shadow_anchor']['y'],
              ],
            ],
          ],
        ],
      ],
    ],
  ]);
  if (!empty($feature_settings['marker_icon_path'])) {
    $iconPath = \Drupal::token()
      ->replace($feature_settings['marker_icon_path'], $context);
    $iconUrl = file_url_transform_relative(file_create_url($iconPath));
    $render_array['#attached']['drupalSettings']['geolocation']['maps'][$render_array['#id']][$this
      ->getPluginId()]['markerIconPath'] = $iconUrl;
  }
  if (!empty($feature_settings['marker_shadow_path'])) {
    $shadowPath = \Drupal::token()
      ->replace($feature_settings['marker_shadow_path'], $context);
    $shadowUrl = file_url_transform_relative(file_create_url($shadowPath));
    $render_array['#attached']['drupalSettings']['geolocation']['maps'][$render_array['#id']][$this
      ->getPluginId()]['markerShadowPath'] = $shadowUrl;
  }
  return $render_array;
}