-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ITKDev: Tried to make local task for plugin configuration
- Loading branch information
Showing
14 changed files
with
450 additions
and
27 deletions.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
os2forms_audit.plugin_settings_tasks: | ||
title: 'Dynamic tasks' | ||
route_name: os2forms_audit.plugin_settings_local_tasks | ||
base_route: os2forms_audit.plugin_settings_local_tasks | ||
deriver: Drupal\os2forms_audit\Plugin\Derivative\LocalTask |
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,3 +1,6 @@ | ||
<?php | ||
|
||
|
||
/** | ||
* @file | ||
* The module file required by all modules. | ||
*/ |
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,7 +1,8 @@ | ||
os2forms_audit.admin_settings: | ||
path: '/admin/config/os2forms_audit/settings' | ||
os2forms_audit.plugin_settings_local_tasks: | ||
path: '/admin/config/os2forms_audit/settings/{type}' | ||
defaults: | ||
_form: '\Drupal\os2forms_audit\Form\SettingsForm' | ||
_title: 'OS2Forms Audit settings' | ||
_controller: '\Drupal\os2forms_audit\Controller\LocalTasksController::dynamicTasks' | ||
_title: 'Dynamic tasks' | ||
type: '' | ||
requirements: | ||
_permission: 'administer site' |
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
55 changes: 55 additions & 0 deletions
55
modules/os2forms_audit/src/Controller/LocalTasksController.php
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,55 @@ | ||
<?php | ||
|
||
namespace Drupal\os2forms_audit\Controller; | ||
|
||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Controller\ControllerBase; | ||
use Drupal\Core\Form\FormBuilderInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class LocalTasksController extends ControllerBase { | ||
|
||
/** | ||
* Default constructor. | ||
* | ||
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder | ||
* Form builder object. | ||
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | ||
* CConfiguration factory. | ||
*/ | ||
public function __construct( | ||
FormBuilderInterface $formBuilder, | ||
ConfigFactoryInterface $configFactory, | ||
) { | ||
$this->formBuilder = $formBuilder; | ||
$this->configFactory = $configFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container): LocalTasksController|static { | ||
return new static( | ||
$container->get('form_builder'), | ||
$container->get('config.factory'), | ||
); | ||
} | ||
|
||
/** | ||
* Get dynamic tasks. | ||
* | ||
* @param string|null $type (optional) | ||
* The type of form to retrieve. Defaults to NULL. | ||
* | ||
* @return array | ||
* An array containing the form definition. | ||
*/ | ||
public function dynamicTasks(string $type = NULL): array { | ||
if (empty($type)) { | ||
return $this->formBuilder->getForm('\Drupal\os2forms_audit\Form\SettingsForm'); | ||
} | ||
|
||
return $this->formBuilder->getForm('\Drupal\os2forms_audit\Form\PluginSettingsForm', $type); | ||
} | ||
|
||
} |
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,123 @@ | ||
<?php | ||
|
||
namespace Drupal\os2forms_audit\Form; | ||
|
||
/** | ||
* @file | ||
* Abstract class for PluginSettingsForm implementation. | ||
*/ | ||
|
||
use Drupal\Component\Plugin\PluginManagerInterface; | ||
use Drupal\Core\Config\ConfigFactoryInterface; | ||
use Drupal\Core\Form\ConfigFormBase; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\os2web_datalookup\Form\PluginSettingsFormInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Class for PluginSettingsForm implementation. | ||
*/ | ||
class PluginSettingsForm extends ConfigFormBase implements PluginSettingsFormInterface { | ||
|
||
/** | ||
* The manager to be used for instantiating plugins. | ||
* | ||
* @var \Drupal\Component\Plugin\PluginManagerInterface | ||
*/ | ||
protected PluginManagerInterface $manager; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct( | ||
ConfigFactoryInterface $config_factory, | ||
PluginManagerInterface $manager | ||
) { | ||
parent::__construct($config_factory); | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container): PluginSettingsForm|ConfigFormBase|static { | ||
return new static( | ||
$container->get('config.factory'), | ||
$container->get('plugin.manager.os2forms_audit_logger') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getConfigName(): string { | ||
return 'os2forms_audit.plugin_settings'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getEditableConfigNames(): array { | ||
return [$this->getConfigName()]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFormId(): string { | ||
return $this->getConfigName() . '_settings_form'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(array $form, FormStateInterface $form_state): array { | ||
$plugin_id = $form_state->getBuildInfo()['args'][0]; | ||
$instance = $this->getPluginInstance($plugin_id); | ||
$form = $instance->buildConfigurationForm($form, $form_state); | ||
|
||
return parent::buildForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validateForm(array &$form, FormStateInterface $form_state): void { | ||
$plugin_id = $form_state->getBuildInfo()['args'][0]; | ||
$instance = $this->getPluginInstance($plugin_id); | ||
$instance->validateConfigurationForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function submitForm(array &$form, FormStateInterface $form_state): void { | ||
$plugin_id = $form_state->getBuildInfo()['args'][0]; | ||
$instance = $this->getPluginInstance($plugin_id); | ||
$instance->submitConfigurationForm($form, $form_state); | ||
|
||
$config = $this->config($this->getConfigName()); | ||
$config->set($plugin_id, $instance->getConfiguration()); | ||
$config->save(); | ||
|
||
parent::submitForm($form, $form_state); | ||
} | ||
|
||
/** | ||
* Returns plugin instance for a given plugin id. | ||
* | ||
* @param string $plugin_id | ||
* The plugin_id for the plugin instance. | ||
* | ||
* @return object | ||
* Plugin instance. | ||
* | ||
* @throws \Drupal\Component\Plugin\Exception\PluginException | ||
*/ | ||
public function getPluginInstance(string $plugin_id): object { | ||
$configuration = $this->config($this->getConfigName())->get($plugin_id); | ||
|
||
return $this->manager->createInstance($plugin_id, $configuration ?? []); | ||
} | ||
|
||
} |
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
Oops, something went wrong.