class MapRenderEventSubscriber in farmOS 2.x
Same name in this branch
- 2.x modules/asset/land/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_land\EventSubscriber\MapRenderEventSubscriber
- 2.x modules/asset/structure/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_structure\EventSubscriber\MapRenderEventSubscriber
- 2.x modules/core/map/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_map\EventSubscriber\MapRenderEventSubscriber
- 2.x modules/core/quantity/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\quantity\EventSubscriber\MapRenderEventSubscriber
- 2.x modules/core/ui/map/src/EventSubscriber/MapRenderEventSubscriber.php \Drupal\farm_ui_map\EventSubscriber\MapRenderEventSubscriber
- 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
- class \Drupal\farm_ui_map\EventSubscriber\MapRenderEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
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
File
- modules/
core/ ui/ map/ src/ EventSubscriber/ MapRenderEventSubscriber.php, line 16
Namespace
Drupal\farm_ui_map\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MapRenderEventSubscriber:: |
protected | property | Asset types. | |
MapRenderEventSubscriber:: |
protected | property | The layer style loader service. | |
MapRenderEventSubscriber:: |
public static | function | ||
MapRenderEventSubscriber:: |
public | function | React to the MapRenderEvent. | |
MapRenderEventSubscriber:: |
public | function | MapRenderEventSubscriber Constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |