FarmMap.php in farmOS 2.x
File
modules/core/map/src/Element/FarmMap.php
View source
<?php
namespace Drupal\farm_map\Element;
use Drupal\Component\Utility\Html;
use Drupal\Core\Render\Element\RenderElement;
use Drupal\farm_map\Event\MapRenderEvent;
class FarmMap extends RenderElement {
public function getInfo() {
$class = get_class($this);
return [
'#pre_render' => [
[
$class,
'preRenderMap',
],
],
'#theme' => 'farm_map',
'#map_type' => 'default',
];
}
public static function preRenderMap(array $element) {
if (empty($element['#attributes']['id'])) {
$map_id = Html::getUniqueId('farm-map-' . $element['#map_type']);
$element['#attributes']['id'] = $map_id;
}
else {
$map_id = $element['#attributes']['id'];
}
$map = \Drupal::entityTypeManager()
->getStorage('map_type')
->load($element['#map_type']);
$element['#attributes']['class'][] = 'farm-map';
if (empty($element['#attributes']['data-map-instantiator'])) {
$element['#attributes']['data-map-instantiator'] = 'farm_map';
}
$element['#attached']['library'][] = 'farm_map/farmOS-map';
$element['#attached']['library'][] = 'farm_map/farm_map';
$map_settings = !empty($element['#map_settings']) ? $element['#map_settings'] : [];
$map_options = $map
->getMapOptions();
$instance_settings = array_merge_recursive($map_settings, $map_options);
$element['#attached']['drupalSettings']['farm_map'][$map_id] = $instance_settings;
$event = new MapRenderEvent($map, $element);
\Drupal::service('event_dispatcher')
->dispatch(MapRenderEvent::EVENT_NAME, $event);
return $event->element;
}
}
Classes
Name |
Description |
FarmMap |
Provides a farm_map render element. |