diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d7a3d0..54ff968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,27 @@ All Notable changes to `drupal/opening-hours` module. +## [8.x-1.1] + +**IMPORTANT : From now on, an API key is required due to a change in the API +endpoint.** + +Change the API endpoint and set the API key value in the webservice +configuration after updating the module to this version. + +See [service documentation](https://developer.gent.be/docs/dataset?service_id=openingsuren_service) +for more information about the endpoint. + +### Changed + +* DMOH-55: Changed the way to access the endpoint: added support to set the + required API key. + +### Fixed + +* DMOH-53: Fixed not unique element wrapper. +* DMOH-53: Fixed detecting if the element was submitted. + ## [8.x-1.0] ### Added @@ -153,6 +174,7 @@ for the same widget. * DMOH-20: Added the opening hours field type. * DMOH-21: Added the opening hours field widget. +[8.x-1.1]: https://github.com/StadGent/drupal_module_opening-hours/compare/8.x-1.0...8.x-1.1 [8.x-1.0]: https://github.com/StadGent/drupal_module_opening-hours/compare/8.x-1.0-beta1...8.x-1.0 [8.x-1.0-beta1]: https://github.com/StadGent/drupal_module_opening-hours/compare/8.x-1.0-alpha16...8.x-1.0-beta1 [8.x-1.0-alpha16]: https://github.com/StadGent/drupal_module_opening-hours/compare/8.x-1.0-alpha15...8.x-1.0-alpha16 diff --git a/README.md b/README.md index 6d38b80..39bba99 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,10 @@ root and add following lines in the `repositories section: "type": "package", "package": { "name": "drupal/opening-hours-widget", - "version": "0.0.3", + "version": "0.1.1", "type": "drupal-library", "dist": { - "url": "https://github.com/StadGent/npm_package_opening-hours-widget/releases/download/v0.0.3/opening-hours-widget.zip", + "url": "https://github.com/StadGent/npm_package_opening-hours-widget/releases/download/0.1.1/opening-hours-widget.zip", "type": "zip" } } diff --git a/composer.json b/composer.json index c5cc7c8..43f570a 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "source": "https://github.com/StadGent/drupal_module_opening-hours" }, "require": { - "stadgent/services-opening-hours": "^1.0" + "stadgent/services-opening-hours": "^1.1" }, "require-dev": { "brainmaestro/composer-git-hooks": "^2.4", diff --git a/js/opening-hours.binding.js b/js/opening-hours.binding.js index 4a4b784..8618982 100644 --- a/js/opening-hours.binding.js +++ b/js/opening-hours.binding.js @@ -20,6 +20,7 @@ var options = { 'endpoint': settings.openingHours.endpoint, + 'endpoint_key': settings.openingHours.endpoint_key, 'language': settings.openingHours.language, 'error' : function (request) { var elem = getClosest(request.element, '.field'); diff --git a/js/opening-hours.opening-hours.js b/js/opening-hours.opening-hours.js index 618d615..7725cba 100644 --- a/js/opening-hours.opening-hours.js +++ b/js/opening-hours.opening-hours.js @@ -21,6 +21,7 @@ function OpeningHours(items, options) { var defaults = { endpoint: '', + endpoint_key: '', language: 'en', requestDate: this.getRequestDateFromUrl() }; @@ -31,15 +32,16 @@ function OpeningHours(items, options) { }); this.settings = defaults; - this.ohw = new OpeningHoursWidget({ - endpoint: this.settings.endpoint - }); - if (!this.settings.endpoint || !this.settings.endpoint.length) { console.error('OpeningHours : Please provide an API endpoint.'); return this; } + this.ohw = new OpeningHoursWidget({ + endpoint: this.settings.endpoint, + endpoint_key: this.settings.endpoint_key + }); + // Render the widgets for all found items. for (var i = 0; i < items.length; i++) { this.renderItemWidget( diff --git a/opening_hours.libraries.yml b/opening_hours.libraries.yml index 1fc1a79..84a7eeb 100644 --- a/opening_hours.libraries.yml +++ b/opening_hours.libraries.yml @@ -1,7 +1,7 @@ library: - version: 0.0.3 + version: 0.1.1 js: - /libraries/opening-hours-widget/dist/opening-hours-widget.min.js: { preprocess: false, minified: true } + /libraries/opening-hours-widget/dist/opening-hours-widget.min.js: { preprocess: false } widget: version: 1.x diff --git a/src/Element/OpeningHoursWidget.php b/src/Element/OpeningHoursWidget.php index 1b96a9c..b6a7292 100644 --- a/src/Element/OpeningHoursWidget.php +++ b/src/Element/OpeningHoursWidget.php @@ -81,6 +81,7 @@ public function getInfo() { '#theme' => 'opening_hours_widget', '#type' => NULL, '#endpoint' => $this->openingHoursConfig->get('endpoint'), + '#endpoint_key' => $this->openingHoursConfig->get('key'), '#service_id' => NULL, '#channel_id' => NULL, '#date' => NULL, @@ -101,6 +102,7 @@ public static function preRenderMyElement($element) { // Attach widget + endpoint configuration. $element['#attached']['library'][] = 'opening_hours/widget'; $element['#attached']['drupalSettings']['openingHours']['endpoint'] = $element['#endpoint']; + $element['#attached']['drupalSettings']['openingHours']['endpoint_key'] = $element['#endpoint_key']; $element['#attached']['drupalSettings']['openingHours']['language'] = $language; return $element; diff --git a/src/Form/ConfigForm.php b/src/Form/ConfigForm.php index 8ca3767..3a69796 100644 --- a/src/Form/ConfigForm.php +++ b/src/Form/ConfigForm.php @@ -58,6 +58,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => TRUE, ]; + $form['key'] = [ + '#type' => 'textfield', + '#title' => $this->t('API key'), + '#description' => $this->t('Provide the API key if it is required to access the service.'), + '#default_value' => $config->get('key'), + '#required' => FALSE, + ]; + $form['cache_enabled'] = [ '#type' => 'checkbox', '#title' => $this->t('Enable cache'), @@ -92,6 +100,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('opening_hours.settings') ->set('endpoint', $form_state->getValue('endpoint')) + ->set('key', $form_state->getValue('key')) ->set('cache_enabled', (int) $form_state->getValue('cache_enabled')) ->save(); diff --git a/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php b/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php index e79d93d..f9c9251 100644 --- a/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php +++ b/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php @@ -2,6 +2,7 @@ namespace Drupal\opening_hours\Plugin\Field\FieldWidget; +use Drupal\Component\Utility\Html; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -143,10 +144,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ? $this->getChannelOptionsForService($currentService) : []; - $wrapperId = sprintf( - 'form-item-wrapper-%s-%d-opening-hours-channel', - $this->fieldDefinition->getName(), - $delta + // Unique channel wrapper id to update using ajax callback. + $wrapperId = Html::getUniqueId( + sprintf('%s-wrapper', $this->fieldDefinition->getName()) ); $element['opening_hours'] = [ @@ -477,7 +477,7 @@ protected function getChannelOptionsForService(Service $service) { */ protected function extractFormStateValues($delta, array $form, FormStateInterface $form_state) { $values = [ - 'is_submitted' => !empty($form_state->getValues()), + 'is_submitted' => $this->isFormSubmitted($form_state), 'service' => NULL, 'channel' => NULL, ]; @@ -504,4 +504,20 @@ protected function extractFormStateValues($delta, array $form, FormStateInterfac return $values; } + /** + * Check if the form was submitted by changing the service. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state to get the triggering element from. + * + * @return bool + * Is submitted. + */ + protected function isFormSubmitted(FormStateInterface $form_state) { + $trigger = $form_state->getTriggeringElement(); + + return !empty($trigger['#autocomplete_route_name']) + && $trigger['#autocomplete_route_name'] === 'opening_hours.service.autocomplete'; + } + } diff --git a/src/Services/ClientService.php b/src/Services/ClientService.php index 8b3b6a2..3321169 100644 --- a/src/Services/ClientService.php +++ b/src/Services/ClientService.php @@ -23,13 +23,13 @@ class ClientService extends StadGentClient { public function __construct(ConfigFactoryInterface $config_factory) { $config = $config_factory->get('opening_hours.settings'); - $configuration = new Configuration($config->get('endpoint')); - $guzzleClient = new GuzzleClient( - [ - 'base_uri' => $configuration->getUri(), - ] + $configuration = new Configuration( + $config->get('endpoint'), + $config->get('key') ); + $guzzleClient = new GuzzleClient(['base_uri' => $configuration->getUri()]); + parent::__construct($guzzleClient, $configuration); } diff --git a/translations/general.pot b/translations/general.pot index 7bc2b0c..bb7b1d8 100644 --- a/translations/general.pot +++ b/translations/general.pot @@ -21,7 +21,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" -"POT-Creation-Date: 2019-01-07 12:05+0100\n" +"POT-Creation-Date: 2019-02-28 08:43+0100\n" "PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n" "Last-Translator: NAME \n" "Language-Team: LANGUAGE \n" @@ -42,7 +42,7 @@ msgstr "" msgid "The opening hours widget plugin is missing. See README.md for instructions on how to download and extract it." msgstr "" -#: modules/contrib/opening_hours/opening_hours.info.yml:0 modules/contrib/opening_hours/opening_hours.links.menu.yml:0 modules/contrib/opening_hours/opening_hours.routing.yml:0 modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:20 +#: modules/contrib/opening_hours/opening_hours.info.yml:0 modules/contrib/opening_hours/opening_hours.links.menu.yml:0 modules/contrib/opening_hours/opening_hours.routing.yml:0 modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:21 msgid "Opening Hours" msgstr "" @@ -91,14 +91,22 @@ msgid "Provide the endpoint URL including the API version number." msgstr "" #: modules/contrib/opening_hours/src/Form/ConfigForm.php:63 -msgid "Enable cache" +msgid "API key" msgstr "" #: modules/contrib/opening_hours/src/Form/ConfigForm.php:64 +msgid "Provide the API key if it is required to access the service." +msgstr "" + +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:71 +msgid "Enable cache" +msgstr "" + +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:72 msgid "This will enable caching of the responses from the API." msgstr "" -#: modules/contrib/opening_hours/src/Form/ConfigForm.php:78 +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:86 msgid "Provide an absolute URL." msgstr "" @@ -246,7 +254,7 @@ msgstr "" msgid "Adds a field to select the Service and its Channel to show its opening hours for." msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:162 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:163 msgid "Service" msgstr "" @@ -254,7 +262,7 @@ msgstr "" msgid "Service label" msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:174 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:176 msgid "Channel" msgstr "" @@ -262,39 +270,39 @@ msgstr "" msgid "Channel label" msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:152 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:153 msgid "Opening hours" msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:153 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:154 msgid "Select a Service and one of its Channels to link this item with Opening Hours." msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:177 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:179 msgid "Select first a Service" msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:183 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:185 msgid "- Select -" msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:272 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:274 msgid "Service does not exists." msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:281 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:283 msgid "Service is required when Channel is set." msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:289 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:291 msgid "Channel is required when Service is set." msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:298 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:300 msgid "Channel does not exists for this Service." msgstr "" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:405;432 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:407;434 msgid "API returned : @message" msgstr "" diff --git a/translations/nl.po b/translations/nl.po index 46dbc2b..3b1e91d 100644 --- a/translations/nl.po +++ b/translations/nl.po @@ -17,8 +17,8 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" -"POT-Creation-Date: 2019-01-07 12:05+0100\n" -"PO-Revision-Date: 2019-01-07 12:05+0100\n" +"POT-Creation-Date: 2019-02-28 08:43+0100\n" +"PO-Revision-Date: 2019-02-28 08:44+0100\n" "Last-Translator: Peter Decuyper \n" "Language-Team: Digipolis \n" "Language: nl\n" @@ -26,7 +26,7 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -"X-Generator: Poedit 2.2\n" +"X-Generator: Poedit 2.2.1\n" #: modules/contrib/opening_hours/opening_hours.install:18 msgid "Installed" @@ -50,7 +50,7 @@ msgstr "" #: modules/contrib/opening_hours/opening_hours.links.menu.yml:0 #: modules/contrib/opening_hours/opening_hours.routing.yml:0 #: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:20 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:21 msgid "Opening Hours" msgstr "Openingsuren" @@ -106,14 +106,22 @@ msgid "Provide the endpoint URL including the API version number." msgstr "Voorzie de endpoint URL inclusief het API versie nummer." #: modules/contrib/opening_hours/src/Form/ConfigForm.php:63 +msgid "API key" +msgstr "API key" + +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:64 +msgid "Provide the API key if it is required to access the service." +msgstr "Geef de API key indien deze vereist is om de service the gebruiken." + +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:71 msgid "Enable cache" msgstr "Caching inschakelen" -#: modules/contrib/opening_hours/src/Form/ConfigForm.php:64 +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:72 msgid "This will enable caching of the responses from the API." msgstr "Dit zal de caching inschakelen op de resultaten van de API." -#: modules/contrib/opening_hours/src/Form/ConfigForm.php:78 +#: modules/contrib/opening_hours/src/Form/ConfigForm.php:86 msgid "Provide an absolute URL." msgstr "Voorzie een absolute URL." @@ -267,7 +275,7 @@ msgstr "" "openingsuren hiervan te kunnen tonen." #: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:162 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:163 msgid "Service" msgstr "Dienst" @@ -276,7 +284,7 @@ msgid "Service label" msgstr "Service label" #: modules/contrib/opening_hours/src/Plugin/Field/FieldType/OpeningHoursItem.php:9 -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:174 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:176 msgid "Channel" msgstr "Kanaal" @@ -284,11 +292,11 @@ msgstr "Kanaal" msgid "Channel label" msgstr "Kanaal label" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:152 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:153 msgid "Opening hours" msgstr "Openingsuren" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:153 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:154 msgid "" "Select a Service and one of its Channels to link this item with Opening " "Hours." @@ -296,31 +304,31 @@ msgstr "" "Selecteer een dienst en één van zijn kanalen om deze te verbinden met zijn " "openingsuren." -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:177 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:179 msgid "Select first a Service" msgstr "Selecteer eerst een Dienst" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:183 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:185 msgid "- Select -" msgstr "- Selecteer -" -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:272 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:274 msgid "Service does not exists." msgstr "Dienst bestaat niet." -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:281 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:283 msgid "Service is required when Channel is set." msgstr "Dienst is verplicht om een kanaal te selecteren." -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:289 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:291 msgid "Channel is required when Service is set." msgstr "Kanaal is verplicht wanneer een Dienst is geselecteerd." -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:298 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:300 msgid "Channel does not exists for this Service." msgstr "Kanaal bastaat niet voor deze Dienst." -#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:405;432 +#: modules/contrib/opening_hours/src/Plugin/Field/FieldWidget/OpeningHoursWidget.php:407;434 msgid "API returned : @message" msgstr "API retourneerde ; @message"