-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: master
Are you sure you want to change the base?
Changes from all commits
3eb04d5
b3ac59e
fe77788
b87def0
916a9c8
60d2b67
8eb7523
effa0f5
1b50caf
f92153e
ce9a350
314ed50
93c7804
8a54dff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
oe_webtools_maps.settings: | ||
title: Webtools Maps | ||
description: 'Configure Webtools Maps.' | ||
route_name: oe_webtools_maps.settings | ||
parent: system.admin_config_system |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
administer webtools maps: | ||
title: 'Administer Webtools Maps' |
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' |
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 |
---|---|---|
|
@@ -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,14 @@ public function settingsSummary() { | |
*/ | ||
public function viewElements(FieldItemListInterface $items, $langcode) { | ||
$element = []; | ||
$config = $this->configFactory->get('oe_webtools_maps.settings'); | ||
// Fallback to version 2.0 for BC. | ||
$map_version = $config->get('map_version') ?? '2.0'; | ||
|
||
foreach ($items as $delta => $item) { | ||
$data_array = [ | ||
'service' => 'map', | ||
'version' => '2.0', | ||
'version' => $map_version, | ||
'map' => [ | ||
'zoom' => $this->getSetting('zoom_level'), | ||
'center' => [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
should be updated according to changes in Webtools Maps UEC for maps 3.0. See this changes: https://github.com/openeuropa/oe_webtools/pull/258/files#diff-d5788593f82f730c0aae0cc4840492e4a812488fa1fb087015bec48283327268L90 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I confirm that, without this change, the marker does not appear on the map. Following that, this change is required: #254 (comment) |
||
|
@@ -85,36 +146,48 @@ public function viewElements(FieldItemListInterface $items, $langcode) { | |
], | ||
]; | ||
|
||
if ($this->getSetting('show_marker')) { | ||
$data_array['layers'] = [ | ||
$markers_data = [ | ||
'type' => 'FeatureCollection', | ||
'features' => [ | ||
[ | ||
'markers' => [ | ||
'type' => 'FeatureCollection', | ||
'features' => [ | ||
[ | ||
'type' => 'Feature', | ||
'properties' => [ | ||
'name' => $this->t('Coordinates'), | ||
'description' => $this->t('Longitude: @lon, Latitude: @lat', [ | ||
'@lon' => $item->get('lon')->getValue(), | ||
'@lat' => $item->get('lat')->getValue(), | ||
]), | ||
], | ||
'geometry' => [ | ||
'type' => 'Point', | ||
'coordinates' => [ | ||
// Even though in other places the Latitude (lat) comes | ||
// first and the Longitude (lon) second, this array | ||
// requires these values to be reversed. | ||
$item->get('lon')->getValue(), | ||
$item->get('lat')->getValue(), | ||
], | ||
], | ||
], | ||
'type' => 'Feature', | ||
'properties' => [ | ||
'name' => $this->t('Coordinates'), | ||
'description' => $this->t('Longitude: @lon, Latitude: @lat', [ | ||
'@lon' => $item->get('lon')->getValue(), | ||
'@lat' => $item->get('lat')->getValue(), | ||
]), | ||
], | ||
'geometry' => [ | ||
'type' => 'Point', | ||
'coordinates' => [ | ||
// Even though in other places the Latitude (lat) comes | ||
// first and the Longitude (lon) second, this array | ||
// requires these values to be reversed. | ||
$item->get('lon')->getValue(), | ||
$item->get('lat')->getValue(), | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
if ($this->getSetting('show_marker')) { | ||
$data_array['layers'] = [ | ||
'markers' => [ | ||
[ | ||
'data' => $markers_data, | ||
], | ||
], | ||
]; | ||
|
||
if ($map_version === '2.0') { | ||
$data_array['layers'] = [ | ||
[ | ||
'markers' => $markers_data, | ||
], | ||
]; | ||
} | ||
} | ||
|
||
$element[$delta] = [ | ||
|
@@ -129,6 +202,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; | ||
|
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we didn't have at all test coverage for oe_webtools_maps probably it makes sense to extend test coverage to assert field formatter settings. See https://github.com/openeuropa/oe_webtools/pull/258/files#diff-93e9dfce3433d92e9cae1a182d4fb153f8b8169475098c75580da33298e5a187R71 |
||
$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]}}'); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@piotrsmykaj no need for "hook_post_update() and set version 2.0 for the existing websites." as we have this here