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..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 @@ -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 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..a50c62f8 --- /dev/null +++ b/modules/oe_webtools_maps/oe_webtools_maps.permissions.yml @@ -0,0 +1,2 @@ +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 new file mode 100644 index 00000000..f21752df --- /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 settings' + requirements: + _permission: 'administer webtools maps' 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']; + } + +} 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..4cf1ef1e 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,13 @@ 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', + // Fallback to version 2.0 for BC. + 'version' => $config->get('map_version') ?? '2.0', 'map' => [ 'zoom' => $this->getSetting('zoom_level'), 'center' => [ @@ -129,6 +189,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; 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]}}'); + } + +}