From 17ba433e8f0ef170833f114a96d833e066a760cb Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 11 Oct 2024 13:04:16 +0300 Subject: [PATCH 1/9] OEL-3178: Add new permission. --- modules/oe_webtools_maps/oe_webtools_maps.permissions.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 modules/oe_webtools_maps/oe_webtools_maps.permissions.yml diff --git a/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml b/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml new file mode 100644 index 00000000..cbc8a1cb --- /dev/null +++ b/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml @@ -0,0 +1,2 @@ +administer webtools maps form: + title: 'Administer Webtools Maps Form' From 99f3f3f74f44ca9bc6251d35fa75f4f68cc2502d Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 11 Oct 2024 13:05:24 +0300 Subject: [PATCH 2/9] OEL-3178: Add configuration form for maps. --- .../oe_webtools_maps.routing.yml | 7 +++ .../src/Form/WebtoolsMapsSettingsForm.php | 59 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 modules/oe_webtools_maps/oe_webtools_maps.routing.yml create mode 100644 modules/oe_webtools_maps/src/Form/WebtoolsMapsSettingsForm.php diff --git a/modules/oe_webtools_maps/oe_webtools_maps.routing.yml b/modules/oe_webtools_maps/oe_webtools_maps.routing.yml new file mode 100644 index 00000000..02d4d984 --- /dev/null +++ b/modules/oe_webtools_maps/oe_webtools_maps.routing.yml @@ -0,0 +1,7 @@ +oe_webtools_maps.settings: + path: '/admin/config/system/oe_webtools_maps' + defaults: + _form: 'Drupal\oe_webtools_maps\Form\WebtoolsMapsSettingsForm' + _title: 'Webtools Maps Form settings' + requirements: + _permission: 'administer webtools maps form' diff --git a/modules/oe_webtools_maps/src/Form/WebtoolsMapsSettingsForm.php b/modules/oe_webtools_maps/src/Form/WebtoolsMapsSettingsForm.php new file mode 100644 index 00000000..522933a6 --- /dev/null +++ b/modules/oe_webtools_maps/src/Form/WebtoolsMapsSettingsForm.php @@ -0,0 +1,59 @@ +config('oe_webtools_maps.settings'); + + $form['map_version'] = [ + '#type' => 'select', + '#title' => $this->t('Map Version'), + '#options' => [ + '2.0' => $this->t('Version 2.0'), + '3.0' => $this->t('Version 3.0'), + ], + '#default_value' => $config->get('map_version') ?? '2.0', + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state): void { + $this->config('oe_webtools_maps.settings') + ->set('map_version', $form_state->getValue('map_version')) + ->save(); + + parent::submitForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames(): array { + return ['oe_webtools_maps.settings']; + } + +} From 1c24c7c41a6d2ed089ea46cf75583b5568afecfc Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 11 Oct 2024 13:05:50 +0300 Subject: [PATCH 3/9] OEL-3178: Add new maps config. --- .../config/install/oe_webtools_maps.settings.yml | 1 + .../config/schema/oe_webtools_maps.schema.yml | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 modules/oe_webtools_maps/config/install/oe_webtools_maps.settings.yml diff --git a/modules/oe_webtools_maps/config/install/oe_webtools_maps.settings.yml b/modules/oe_webtools_maps/config/install/oe_webtools_maps.settings.yml new file mode 100644 index 00000000..c145e4dc --- /dev/null +++ b/modules/oe_webtools_maps/config/install/oe_webtools_maps.settings.yml @@ -0,0 +1 @@ +map_version: '3.0' diff --git a/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml b/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml index f7f43ded..6963f86c 100644 --- a/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml +++ b/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml @@ -1,3 +1,11 @@ +oe_webtools_maps.settings: + type: config_object + label: 'Webtools Maps Settings' + mapping: + map_version: + type: string + label: 'Map Version' + field.formatter.settings.oe_webtools_maps_map: type: mapping label: Webtools Map formatter settings From d116463f82d0aafa54fb5d42d55e856e2613a5ff Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 11 Oct 2024 13:06:33 +0300 Subject: [PATCH 4/9] OEL-3178: Use new config in maps formatter. --- .../FieldFormatter/WebtoolsMapFormatter.php | 65 ++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php b/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php index c10ffd7a..ff9067ea 100644 --- a/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php +++ b/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php @@ -4,10 +4,14 @@ namespace Drupal\oe_webtools_maps\Plugin\Field\FieldFormatter; +use Drupal\Core\Cache\CacheableMetadata; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\oe_webtools_maps\Component\Render\JsonEncoded; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Displays a Geofield as a map using the Webtools Maps service. @@ -22,6 +26,60 @@ */ class WebtoolsMapFormatter extends FormatterBase { + /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + + /** + * Constructs a WebtoolsMapFormatter object. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Any third party settings. + * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory + * The config factory. + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ConfigFactoryInterface $configFactory) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + + $this->fieldDefinition = $field_definition; + $this->settings = $settings; + $this->label = $label; + $this->viewMode = $view_mode; + $this->thirdPartySettings = $third_party_settings; + $this->configFactory = $configFactory; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('config.factory') + ); + } + /** * {@inheritdoc} */ @@ -71,11 +129,12 @@ public function settingsSummary() { */ public function viewElements(FieldItemListInterface $items, $langcode) { $element = []; + $config = $this->configFactory->get('oe_webtools_maps.settings'); foreach ($items as $delta => $item) { $data_array = [ 'service' => 'map', - 'version' => '2.0', + 'version' => $config->get('map_version') ?? '2.0', 'map' => [ 'zoom' => $this->getSetting('zoom_level'), 'center' => [ @@ -129,6 +188,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $element['#attached'] = [ 'library' => ['oe_webtools/drupal.webtools-smartloader'], ]; + + $cache_metadata = new CacheableMetadata(); + $cache_metadata->addCacheableDependency($config); + $cache_metadata->applyTo($element); } return $element; From 0d8da59985d198bd7ab778c3fd83ed22fc895f00 Mon Sep 17 00:00:00 2001 From: drishu Date: Tue, 22 Oct 2024 11:34:02 +0300 Subject: [PATCH 5/9] OEL-3178: Cover maps configuration with tests. --- .../src/Functional/ConfigurationTest.php | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 modules/oe_webtools_maps/tests/src/Functional/ConfigurationTest.php diff --git a/modules/oe_webtools_maps/tests/src/Functional/ConfigurationTest.php b/modules/oe_webtools_maps/tests/src/Functional/ConfigurationTest.php new file mode 100644 index 00000000..3d8389ae --- /dev/null +++ b/modules/oe_webtools_maps/tests/src/Functional/ConfigurationTest.php @@ -0,0 +1,128 @@ +drupalCreateContentType([ + 'type' => 'test', + ]); + $field_storage = FieldStorageConfig::create([ + 'field_name' => 'geofield_field', + 'entity_type' => 'node', + 'type' => 'geofield', + 'settings' => [ + 'backend' => 'geofield_backend_default', + ], + ]); + $field_storage->save(); + + FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => 'test', + 'settings' => [ + 'backend' => 'geofield_backend_default', + ], + ])->save(); + + /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */ + $display = \Drupal::entityTypeManager() + ->getStorage('entity_view_display') + ->load('node.test.default'); + + $display->setComponent('geofield_field', [ + 'label' => 'above', + 'type' => 'oe_webtools_maps_map', + ]); + $display->save(); + + EntityFormDisplay::load('node.test.default') + ->setComponent($field_storage->getName(), [ + 'type' => 'geofield_latlon', + ]) + ->save(); + + $this->drupalLogin($this->drupalCreateUser([], NULL, TRUE)); + } + + /** + * Tests if changing configuration changes the map JSON. + */ + public function testConfigurationChanges(): void { + $node = Node::create([ + 'type' => 'test', + 'user_id' => 1, + 'title' => 'My map', + 'geofield_field' => [ + [ + 'value' => 'POINT (-2.1021 42.2257)', + ], + ], + ]); + $node->save(); + + $this->drupalGet('/node/1'); + + // New installations receive map version 3.0 by default. + $this->assertBodyContainsApplicationJson('{"service":"map","version":"3.0","map":{"zoom":4,"center":[42.2257,-2.1021]}}'); + + // Change the config, assert the map changed. + $this->drupalGet('admin/config/system/oe_webtools_maps'); + $page = $this->getSession()->getPage(); + $page->selectFieldOption('Map Version', '2.0'); + $page->pressButton('Save configuration'); + + $this->drupalGet('/node/1'); + $this->assertBodyContainsApplicationJson('{"service":"map","version":"2.0","map":{"zoom":4,"center":[42.2257,-2.1021]}}'); + + // Change it back to version 3.0. + $this->drupalGet('admin/config/system/oe_webtools_maps'); + $page = $this->getSession()->getPage(); + $page->selectFieldOption('Map Version', '3.0'); + $page->pressButton('Save configuration'); + + $this->drupalGet('/node/1'); + $this->assertBodyContainsApplicationJson('{"service":"map","version":"3.0","map":{"zoom":4,"center":[42.2257,-2.1021]}}'); + + // Delete the config to emulate an upgrade scenario. + \Drupal::configFactory()->getEditable('oe_webtools_maps.settings') + ->delete(); + $this->drupalGet('/node/1'); + $this->assertBodyContainsApplicationJson('{"service":"map","version":"2.0","map":{"zoom":4,"center":[42.2257,-2.1021]}}'); + } + +} From 20448e7eb7cbdd08cfa8f8e1c4130a32508d9cbe Mon Sep 17 00:00:00 2001 From: drishu Date: Tue, 22 Oct 2024 11:34:30 +0300 Subject: [PATCH 6/9] OEL-3178: Comment BC fallback. --- .../src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php b/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php index ff9067ea..4cf1ef1e 100644 --- a/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php +++ b/modules/oe_webtools_maps/src/Plugin/Field/FieldFormatter/WebtoolsMapFormatter.php @@ -134,6 +134,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { foreach ($items as $delta => $item) { $data_array = [ 'service' => 'map', + // Fallback to version 2.0 for BC. 'version' => $config->get('map_version') ?? '2.0', 'map' => [ 'zoom' => $this->getSetting('zoom_level'), From 09aa5f4cbe2a93200db6331d0fc1e21b5786c7e8 Mon Sep 17 00:00:00 2001 From: drishu Date: Mon, 4 Nov 2024 17:46:48 +0200 Subject: [PATCH 7/9] OEL-3178: Rename permission. --- modules/oe_webtools_maps/oe_webtools_maps.permissions.yml | 4 ++-- modules/oe_webtools_maps/oe_webtools_maps.routing.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml b/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml index cbc8a1cb..a50c62f8 100644 --- a/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml +++ b/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml @@ -1,2 +1,2 @@ -administer webtools maps form: - title: 'Administer Webtools Maps Form' +administer webtools maps: + title: 'Administer Webtools Maps' diff --git a/modules/oe_webtools_maps/oe_webtools_maps.routing.yml b/modules/oe_webtools_maps/oe_webtools_maps.routing.yml index 02d4d984..29dc0f28 100644 --- a/modules/oe_webtools_maps/oe_webtools_maps.routing.yml +++ b/modules/oe_webtools_maps/oe_webtools_maps.routing.yml @@ -4,4 +4,4 @@ oe_webtools_maps.settings: _form: 'Drupal\oe_webtools_maps\Form\WebtoolsMapsSettingsForm' _title: 'Webtools Maps Form settings' requirements: - _permission: 'administer webtools maps form' + _permission: 'administer webtools maps' From 6aee967bb458ce1526a1e2be7e1683e4a7a654e3 Mon Sep 17 00:00:00 2001 From: drishu Date: Mon, 4 Nov 2024 17:47:55 +0200 Subject: [PATCH 8/9] OEL-3178: Drop shorten form page title. --- modules/oe_webtools_maps/oe_webtools_maps.routing.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/oe_webtools_maps/oe_webtools_maps.routing.yml b/modules/oe_webtools_maps/oe_webtools_maps.routing.yml index 29dc0f28..f21752df 100644 --- a/modules/oe_webtools_maps/oe_webtools_maps.routing.yml +++ b/modules/oe_webtools_maps/oe_webtools_maps.routing.yml @@ -2,6 +2,6 @@ oe_webtools_maps.settings: path: '/admin/config/system/oe_webtools_maps' defaults: _form: 'Drupal\oe_webtools_maps\Form\WebtoolsMapsSettingsForm' - _title: 'Webtools Maps Form settings' + _title: 'Webtools Maps settings' requirements: _permission: 'administer webtools maps' From 030e74309e1fc5b895fce73d2004f3b745e39be1 Mon Sep 17 00:00:00 2001 From: drishu Date: Mon, 4 Nov 2024 17:48:49 +0200 Subject: [PATCH 9/9] OEL-3178: Improve grammer in schema label. --- .../oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml b/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml index 6963f86c..d6204352 100644 --- a/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml +++ b/modules/oe_webtools_maps/config/schema/oe_webtools_maps.schema.yml @@ -4,7 +4,7 @@ oe_webtools_maps.settings: mapping: map_version: type: string - label: 'Map Version' + label: 'Map version' field.formatter.settings.oe_webtools_maps_map: type: mapping