Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OEL-3178: Add maps version 3.0 #254

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
map_version: '3.0'
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions modules/oe_webtools_maps/oe_webtools_maps.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
administer webtools maps:
title: 'Administer Webtools Maps'
7 changes: 7 additions & 0 deletions modules/oe_webtools_maps/oe_webtools_maps.routing.yml
Original file line number Diff line number Diff line change
@@ -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'
59 changes: 59 additions & 0 deletions modules/oe_webtools_maps/src/Form/WebtoolsMapsSettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Drupal\oe_webtools_maps\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Provides configuration form for the Maps webtools widget.
*/
class WebtoolsMapsSettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'oe_webtools_maps_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->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'];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -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;
Expand Down
128 changes: 128 additions & 0 deletions modules/oe_webtools_maps/tests/src/Functional/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\oe_webtools_maps\Functional;

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\oe_webtools\Traits\ApplicationJsonAssertTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;

/**
* Tests that the configured settings are correctly reflected in the page.
*/
class ConfigurationTest extends BrowserTestBase {

use ApplicationJsonAssertTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'geofield',
'node',
'oe_webtools_maps',
];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

$this->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]}}');
}

}