From 4574cc5e9bc9e2d4b5bac89f21721bf85a0fc866 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Thu, 16 Jun 2022 21:08:32 -0400 Subject: [PATCH] Allow #behaviors to be added directly to farm_map and farm_map_input elements. --- modules/core/map/src/Element/FarmMap.php | 12 ++++++++++++ modules/core/map/src/Element/FarmMapInput.php | 2 ++ modules/core/map/src/Event/MapRenderEvent.php | 2 ++ 3 files changed, 16 insertions(+) diff --git a/modules/core/map/src/Element/FarmMap.php b/modules/core/map/src/Element/FarmMap.php index d4321a620..8f9ae48e7 100644 --- a/modules/core/map/src/Element/FarmMap.php +++ b/modules/core/map/src/Element/FarmMap.php @@ -25,6 +25,7 @@ class FarmMap extends RenderElement { '#theme' => 'farm_map', '#map_type' => 'default', '#map_settings' => [], + '#behaviors' => [], ]; } @@ -38,6 +39,8 @@ class FarmMap extends RenderElement { * * @return array * A renderable array representing the map. + * + * @see \Drupal\farm_map\Event\MapRenderEvent */ public static function preRenderMap(array $element) { @@ -67,6 +70,15 @@ class FarmMap extends RenderElement { $element['#attached']['library'][] = 'farm_map/farmOS-map'; $element['#attached']['library'][] = 'farm_map/farm_map'; + // If #behaviors are included, attach each one. + foreach ($element['#behaviors'] as $behavior_name) { + /** @var \Drupal\farm_map\Entity\MapBehaviorInterface $behavior */ + $behavior = \Drupal::entityTypeManager()->getStorage('map_behavior')->load($behavior_name); + if (!empty($behavior)) { + $element['#attached']['library'][] = $behavior->getLibrary(); + } + } + // Include the map options. $map_options = $map->getMapOptions(); diff --git a/modules/core/map/src/Element/FarmMapInput.php b/modules/core/map/src/Element/FarmMapInput.php index 027119f03..3ea23df64 100644 --- a/modules/core/map/src/Element/FarmMapInput.php +++ b/modules/core/map/src/Element/FarmMapInput.php @@ -36,6 +36,7 @@ class FarmMapInput extends FormElement { '#description_display' => 'before', '#map_type' => 'geofield_widget', '#map_settings' => [], + '#behaviors' => [], '#default_value' => '', '#display_raw_geometry' => TRUE, ]; @@ -75,6 +76,7 @@ class FarmMapInput extends FormElement { '#type' => 'farm_map', '#map_type' => $element['#map_type'], '#map_settings' => $map_settings, + '#behaviors' => $element['#behaviors'], ]; // Add a textarea for the WKT value. diff --git a/modules/core/map/src/Event/MapRenderEvent.php b/modules/core/map/src/Event/MapRenderEvent.php index 3073cb2b8..5829ad075 100644 --- a/modules/core/map/src/Event/MapRenderEvent.php +++ b/modules/core/map/src/Event/MapRenderEvent.php @@ -72,6 +72,8 @@ class MapRenderEvent extends Event { * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * + * @see \Drupal\farm_map\Element\FarmMap */ public function addBehavior(string $behavior_name, array $settings = []) {