Skip to content

Commit 5111bd6

Browse files
committed
Module general implementation
1 parent f67d067 commit 5111bd6

14 files changed

+519
-0
lines changed

.travis.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
language: php
2+
3+
sudo: false
4+
5+
php:
6+
- 7.2
7+
8+
install:
9+
- composer global require drush/drush:8.x-dev drupal/coder mglaman/drupal-check friendsoftwig/twigcs
10+
- export PATH="$HOME/.config/composer/vendor/bin:$PATH"
11+
- phpcs --config-set installed_paths ../../drupal/coder/coder_sniffer
12+
- phpenv rehash
13+
- nvm install node
14+
- nvm use node
15+
- npm install --global yarn
16+
- cd ../ && drush dl drupal-8 --drupal-project-rename=drupal
17+
- cd drupal
18+
- DRUPAL_ROOT=$(pwd)
19+
- export REPOSITORIES='"repositories":\ \['
20+
- export REPOSITORIES_REPLACE='"repositories":\[\{"type":"path","url":"..\/os2web_datalookup","options":\{"symlink":false\}\},'
21+
- export REQUIRE='"require":\ {'
22+
- export REQUIRE_REPLACE='"require":{"os2web\/os2web_datalookup":"\*",'
23+
- sed -i "s/$REPOSITORIES/$REPOSITORIES_REPLACE/g" composer.json
24+
- sed -i "s/$REQUIRE/$REQUIRE_REPLACE/g" composer.json
25+
- composer update
26+
- PROJECT_PATH=$DRUPAL_ROOT/modules/contrib/os2web_datalookup
27+
- cd $DRUPAL_ROOT/core
28+
- yarn install
29+
- npm install --global eslint-config-drupal-bundle stylelint
30+
31+
script:
32+
- phpcs --standard=Drupal --ignore=*.md $PROJECT_PATH
33+
- twigcs $PROJECT_PATH
34+
- cd $DRUPAL_ROOT/core
35+
- eslint $PROJECT_PATH
36+
- stylelint --aei $PROJECT_PATH/**/*.css
37+
- drupal-check $PROJECT_PATH

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "os2web/os2web_datalookup",
3+
"type": "drupal-module",
4+
"description": "Drupal 8 OS2Form module provides advanced webform functionality for Danish Municipalities",
5+
"minimum-stability": "dev",
6+
"prefer-stable": true,
7+
"license": "EUPL-1.2",
8+
"repositories": {
9+
"drupal": {
10+
"type": "composer",
11+
"url": "https://packages.drupal.org/8"
12+
},
13+
"assets": {
14+
"type": "composer",
15+
"url": "https://asset-packagist.org"
16+
}
17+
}
18+
}

os2web_datalookup.info.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'OS2Web datalookup'
2+
type: module
3+
description: 'Provides integration with Danish data lookup services such as Service platformen or Datafordeler.'
4+
package: OS2
5+
core: 8.x

os2web_datalookup.routing.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
os2web_datalookup.status:
2+
path: '/admin/config/system/os2web-datalookup'
3+
defaults:
4+
_controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
5+
_title: 'OS2Web Datalookup'
6+
requirements:
7+
_permission: 'administer os2web datalookup configuration'
8+
9+
route_callbacks:
10+
- '\Drupal\os2web_datalookup\Routing\DataLookupRoutes::routes'

os2web_datalookup.services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
plugin.manager.os2web_datalookup:
3+
class: Drupal\os2web_datalookup\Plugin\DataLookupManager
4+
parent: default_plugin_manager

src/Annotation/DataLookup.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Annotation;
4+
5+
use Drupal\Component\Annotation\Plugin;
6+
7+
/**
8+
* Defines a AuthProvider annotation object.
9+
*
10+
* Plugin Namespace: Plugin/os2web/DataLookup.
11+
*
12+
* @see hook_os2web_nemlogin_auth_provider_info_alter()
13+
* @see \Drupal\os2web_datalookup\Plugin\DataLookupManager
14+
* @see plugin_api
15+
*
16+
* @Annotation
17+
*/
18+
class DataLookup extends Plugin {
19+
20+
/**
21+
* The plugin ID.
22+
*
23+
* @var string
24+
*/
25+
public $id;
26+
27+
/**
28+
* The human-readable name of the consent storage.
29+
*
30+
* @var \Drupal\Core\Annotation\Translation
31+
*
32+
* @ingroup plugin_translatable
33+
*/
34+
public $label;
35+
36+
/**
37+
* A brief description of the consent storage.
38+
*
39+
* This will be shown when adding or configuring this consent storage.
40+
*
41+
* @var \Drupal\Core\Annotation\Translation
42+
*
43+
* @ingroup plugin_translatable
44+
*/
45+
public $description = '';
46+
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Form;
4+
5+
use Drupal\Component\Plugin\PluginManagerInterface;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
9+
/**
10+
* Form or configuring DataLookup plugins.
11+
*/
12+
class DataLookupPluginSettingsForm extends PluginSettingsFormBase {
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function __construct(ConfigFactoryInterface $config_factory, PluginManagerInterface $manager) {
18+
parent::__construct($config_factory);
19+
$this->manager = $manager;
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public static function create(ContainerInterface $container) {
26+
return new static(
27+
$container->get('config.factory'),
28+
$container->get('plugin.manager.os2web_datalookup')
29+
);
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public static function getConfigName() {
36+
return 'os2web_datalookup.settings';
37+
}
38+
39+
}

src/Form/PluginSettingsFormBase.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Form;
4+
5+
/**
6+
* @file
7+
* Abstract class for PluginSettingsForm implementation.
8+
*/
9+
10+
use Drupal\Core\Form\ConfigFormBase;
11+
use Drupal\Core\Form\FormStateInterface;
12+
13+
/**
14+
* Abstract class for PluginSettingsForm implementation.
15+
*/
16+
abstract class PluginSettingsFormBase extends ConfigFormBase implements PluginSettingsFormInterface {
17+
18+
/**
19+
* The manager to be used for instantiating plugins.
20+
*
21+
* @var \Drupal\Component\Plugin\PluginManagerInterface
22+
*/
23+
protected $manager;
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function getEditableConfigNames() {
29+
return [$this->getConfigId()];
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function getFormId() {
36+
return $this->getConfigName() . '_settings_form_' . $this->getPluginIdFromRequest();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function buildForm(array $form, FormStateInterface $form_state) {
43+
$plugin_id = $this->getPluginIdFromRequest();
44+
$instance = $this->getPluginInstance($plugin_id);
45+
$form = $instance->buildConfigurationForm($form, $form_state);
46+
return parent::buildForm($form, $form_state);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function validateForm(array &$form, FormStateInterface $form_state) {
53+
$plugin_id = $this->getPluginIdFromRequest();
54+
$instance = $this->getPluginInstance($plugin_id);
55+
$instance->validateConfigurationForm($form, $form_state);
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
*/
61+
public function submitForm(array &$form, FormStateInterface $form_state) {
62+
$plugin_id = $this->getPluginIdFromRequest();
63+
$instance = $this->getPluginInstance($plugin_id);
64+
$instance->submitConfigurationForm($form, $form_state);
65+
66+
$config = $this->config($this->getConfigId());
67+
foreach ($instance->getConfiguration() as $key => $value) {
68+
$config->set($key, $value);
69+
}
70+
$config->save();
71+
72+
parent::submitForm($form, $form_state);
73+
}
74+
75+
/**
76+
* Returns the value of the param _plugin_id for the current request.
77+
*/
78+
protected function getPluginIdFromRequest() {
79+
$request = $this->getRequest();
80+
return $request->get('_plugin_id');
81+
}
82+
83+
/**
84+
* Returns plugin instance for a given plugin id.
85+
*
86+
* @param string $plugin_id
87+
* The plugin_id for the plugin instance.
88+
*
89+
* @return object
90+
* Plugin instance.
91+
*
92+
* @throws PluginException
93+
*/
94+
public function getPluginInstance($plugin_id) {
95+
$configuration = $this->config($this->getConfigId())->get();
96+
$instance = $this->manager->createInstance($plugin_id, $configuration);
97+
return $instance;
98+
}
99+
100+
/**
101+
* Defines name of the configuration object.
102+
*
103+
* @return string
104+
* Configuration object name.
105+
*/
106+
protected function getConfigId() {
107+
return $this->getConfigName() . '.' . $this->getPluginIdFromRequest();
108+
}
109+
110+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Form;
4+
5+
/**
6+
* Provides an interface for a Plugin Settings Form.
7+
*
8+
* @ingroup form_api
9+
*/
10+
interface PluginSettingsFormInterface {
11+
12+
/**
13+
* Name of the configuration.
14+
*
15+
* @return string
16+
* Configuration name for plugins.
17+
*/
18+
public static function getConfigName();
19+
20+
}

src/Plugin/DataLookupManager.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Drupal\os2web_datalookup\Plugin;
4+
5+
use Drupal\Core\Plugin\DefaultPluginManager;
6+
use Drupal\Core\Cache\CacheBackendInterface;
7+
use Drupal\Core\Extension\ModuleHandlerInterface;
8+
9+
/**
10+
* DataLookupManager plugin manager.
11+
*
12+
* @see \Drupal\os2web_datalookup\Annotation\DataLookup
13+
* @see \Drupal\os2web_datalookup\Plugin\DataLookupInterface
14+
* @see plugin_api
15+
*/
16+
class DataLookupManager extends DefaultPluginManager {
17+
18+
/**
19+
* Constructs an DataLookupManager object.
20+
*
21+
* @param \Traversable $namespaces
22+
* An object that implements \Traversable which contains the root paths
23+
* keyed by the corresponding namespace to look for plugin implementations.
24+
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
25+
* Cache backend instance to use.
26+
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
27+
* The module handler to invoke the alter hook with.
28+
*/
29+
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
30+
parent::__construct(
31+
'Plugin/os2web/DataLookup',
32+
$namespaces,
33+
$module_handler,
34+
'Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupInterface',
35+
'Drupal\os2web_datalookup\Annotation\DataLookup');
36+
37+
$this->alterInfo('os2web_datalookup_info');
38+
$this->setCacheBackend($cache_backend, 'os2web_datalookup');
39+
}
40+
41+
}

0 commit comments

Comments
 (0)