You are here

class MapRenderEventSubscriber in farmOS 2.x

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

An event subscriber for the MapRenderEvent.

Adds the wkt and geofield behaviors to necessary maps.

Hierarchy

Expanded class hierarchy of MapRenderEventSubscriber

1 string reference to 'MapRenderEventSubscriber'
farm_ui_map.services.yml in modules/core/ui/map/farm_ui_map.services.yml
modules/core/ui/map/farm_ui_map.services.yml
1 service uses MapRenderEventSubscriber
farm_ui_map.map_render_event_subscriber in modules/core/ui/map/farm_ui_map.services.yml
Drupal\farm_ui_map\EventSubscriber\MapRenderEventSubscriber

File

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

Namespace

Drupal\farm_ui_map\EventSubscriber
View source
class MapRenderEventSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * Asset types.
   *
   * @var \Drupal\asset\Entity\AssetTypeInterface[]
   */
  protected $assetTypes;

  /**
   * The layer style loader service.
   *
   * @var \Drupal\farm_map\layerStyleLoader
   */
  protected $layerStyleLoader;

  /**
   * MapRenderEventSubscriber Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\farm_map\layerStyleLoaderInterface $layer_style_loader
   *   The layer style loader service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, layerStyleLoaderInterface $layer_style_loader) {
    $this->assetTypes = $entity_type_manager
      ->getStorage('asset_type')
      ->loadMultiple();
    $this->layerStyleLoader = $layer_style_loader;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      MapRenderEvent::EVENT_NAME => 'onMapRender',
    ];
  }

  /**
   * React to the MapRenderEvent.
   *
   * @param \Drupal\farm_map\Event\MapRenderEvent $event
   *   The MapRenderEvent.
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MapRenderEventSubscriber::$assetTypes protected property Asset types.
MapRenderEventSubscriber::$layerStyleLoader protected property The layer style loader service.
MapRenderEventSubscriber::getSubscribedEvents public static function
MapRenderEventSubscriber::onMapRender public function React to the MapRenderEvent.
MapRenderEventSubscriber::__construct public function MapRenderEventSubscriber Constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.