Skip to content

Commit

Permalink
Merge pull request #1 from StadGent/feature/DMOH-19
Browse files Browse the repository at this point in the history
DMOH-19: Added service configuration form.
  • Loading branch information
zero2one authored Dec 11, 2017
2 parents ee986cc + 4e771c1 commit 075df8c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ All Notable changes to `drupal/opening-hours` module.
## [Unreleased]

### Added
* DMOH-19: Added service configuration form.

[Unreleased]: https://github.com/StadGent/drupal_module_opening-hours/compare/master...develop
3 changes: 2 additions & 1 deletion opening_hours.info.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: 'Opening Hours'
type: module
description: 'Integrates the Opening Hours platform functionality.'
package: 'Stad Gent'
configure: opening_hours.admin_config
package: 'Web services'
core: '8.x'
5 changes: 5 additions & 0 deletions opening_hours.links.menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
opening_hours.admin_config:
title: 'Opening Hours'
parent: system.admin_config_services
description: 'Configure the Opening Hours service.'
route_name: opening_hours.admin_config
4 changes: 4 additions & 0 deletions opening_hours.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
configure opening hours:
title: 'Configure Opening Hours Service'
description: 'Give access to the Opening Hours service configuration page.'
restrict access: TRUE
7 changes: 7 additions & 0 deletions opening_hours.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opening_hours.admin_config:
path: admin/config/services/opening-hours
defaults:
_form: \Drupal\opening_hours\Form\ConfigForm
_title: 'Opening Hours'
requirements:
_permission: 'configure opening hours'
108 changes: 108 additions & 0 deletions src/Form/ConfigForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Drupal\opening_hours\Form;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Form to configure the Opening Hours settings.
*/
class ConfigForm extends ConfigFormBase {

/**
* Constructs a ConfigForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
* The String translations.
*/
public function __construct(ConfigFactoryInterface $config_factory, TranslationInterface $stringTranslation) {
parent::__construct($config_factory);
$this->setStringTranslation($stringTranslation);
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('string_translation')
);
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'opening_hours_config';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('opening_hours.settings');

$form['endpoint'] = [
'#type' => 'textfield',
'#title' => $this->t('Endpoint URL'),
'#description' => $this->t('Provide the endpoint URL including the API version number.'),
'#default_value' => $config->get('endpoint'),
'#required' => TRUE,
];

$form['cache_enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable cache'),
'#description' => $this->t('This will enable caching of the responses from the API.'),
'#default_value' => $config->get('cache_enabled'),
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!UrlHelper::isValid($form_state->getValue('endpoint'), TRUE)) {
$form_state->setErrorByName(
'endpoint',
$this->t('Provide an absolute URL.')
);
}
else {
$form_state->setValue(
'endpoint',
rtrim($form_state->getValue('endpoint'), '/')
);
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('opening_hours.settings')
->set('endpoint', $form_state->getValue('endpoint'))
->set('cache_enabled', (int) $form_state->getValue('cache_enabled'))
->save();

parent::submitForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['opening_hours.settings'];
}

}

0 comments on commit 075df8c

Please sign in to comment.