-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from StadGent/feature/DMOH-19
DMOH-19: Added service configuration form.
- Loading branch information
Showing
6 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
|
||
} |