You are here

public function MapRenderEventSubscriber::onMapRender in farmOS 2.x

Same name in this branch
  1. 2.x modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_land\EventSubscriber\MapRenderEventSubscriber::onMapRender()
  2. 2.x modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_structure\EventSubscriber\MapRenderEventSubscriber::onMapRender()
  3. 2.x modules/core/map/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_map\EventSubscriber\MapRenderEventSubscriber::onMapRender()
  4. 2.x modules/core/quantity/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\quantity\EventSubscriber\MapRenderEventSubscriber::onMapRender()
  5. 2.x modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_ui_map\EventSubscriber\MapRenderEventSubscriber::onMapRender()
  6. 2.x modules/core/map/modules/mapbox/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_map_mapbox\EventSubscriber\MapRenderEventSubscriber::onMapRender()

React to the MapRenderEvent.

Parameters

\Drupal\farm_map\Event\MapRenderEvent $event: The MapRenderEvent.

File

modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php, line 62

Class

MapRenderEventSubscriber
An event subscriber for the MapRenderEvent.

Namespace

Drupal\farm_ui_map\EventSubscriber

Code

public function onMapRender(MapRenderEvent $event) {

  // Get the map ID.
  $map_id = $event
    ->getmapType()
    ->id();

  // Add behaviors/settings to default and geofield maps.
  if (in_array($map_id, [
    'default',
    'geofield',
    'geofield_widget',
  ])) {

    // Add "All locations" layers.
    $event
      ->addBehavior('asset_type_layers');
    $settings[$event
      ->getMapTargetId()]['asset_type_layers']['all_locations'] = [
      'label' => $this
        ->t('All locations'),
      'filters' => [
        'is_location' => 1,
      ],
      'color' => 'grey',
      'zoom' => TRUE,
    ];
    $event
      ->addSettings($settings);

    // Prevent zooming to the "All locations" layer if WKT is provided.
    if (!empty($event->element['#map_settings']['wkt'])) {
      $settings[$event
        ->getMapTargetId()]['asset_type_layers']['all_locations']['zoom'] = FALSE;
      $event
        ->addSettings($settings);
    }
  }
  elseif ($map_id == 'dashboard') {
    $layers = [];

    // Define common layer properties.
    $group = $this
      ->t('Locations');
    $filters = [
      'is_location' => 1,
    ];

    // Add layer for all asset types are locations by default.
    foreach ($this->assetTypes as $type) {

      // Only add a layer if the asset type is a location by default.
      if ($type
        ->getThirdPartySetting('farm_location', 'is_location', FALSE)) {

        // Load the map layer style.

        /** @var \Drupal\farm_map\Entity\LayerStyleInterface $layer_style */
        $layer_style = $this->layerStyleLoader
          ->load([
          'asset_type' => $type
            ->id(),
        ]);
        if (!empty($layer_style)) {
          $color = $layer_style
            ->get('color');
        }

        // Add layer for the asset type.
        $layers[$type
          ->id()] = [
          'group' => $group,
          'label' => $type
            ->label(),
          'asset_type' => $type
            ->id(),
          'filters' => $filters,
          'color' => $color ?? 'orange',
          'zoom' => TRUE,
        ];
      }
    }

    // Add the asset_type_layers behavior.
    $event
      ->addBehavior('asset_type_layers');

    // Add map specific settings.
    $settings[$event
      ->getMapTargetId()]['asset_type_layers'] = $layers;
    $event
      ->addSettings($settings);
  }
}