Skip to content

Commit

Permalink
ITKDev: Tried to make local task for plugin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Apr 18, 2024
1 parent 27b2579 commit 7b0ca43
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 27 deletions.
2 changes: 1 addition & 1 deletion modules/os2forms_audit/os2forms_audit.links.menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ os2forms_audit.admin_settings:
title: 'OS2Forms Audit settings'
parent: system.admin_config_system
description: 'Settings for the OS2Forms Audit module'
route_name: os2forms_audit.admin_settings
route_name: os2forms_audit.plugin_settings_local_tasks
5 changes: 5 additions & 0 deletions modules/os2forms_audit/os2forms_audit.links.task.yml
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
5 changes: 4 additions & 1 deletion modules/os2forms_audit/os2forms_audit.module
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<?php


/**
* @file
* The module file required by all modules.
*/
9 changes: 5 additions & 4 deletions modules/os2forms_audit/os2forms_audit.routing.yml
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'
5 changes: 3 additions & 2 deletions modules/os2forms_audit/src/Annotation/AuditLoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\os2forms_audit\Annotation;

use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;

/**
* Defines a AuditLoggerProvider annotation object.
Expand All @@ -27,7 +28,7 @@ class AuditLoggerProvider extends Plugin {
*
* @ingroup plugin_translatable
*/
public \Drupal\Core\Annotation\Translation $title;
public Translation $title;

/**
* A brief description of the consent storage.
Expand All @@ -38,6 +39,6 @@ class AuditLoggerProvider extends Plugin {
*
* @ingroup plugin_translatable
*/
public \Drupal\Core\Annotation\Translation|string $description = '';
public Translation|string $description = '';

}
55 changes: 55 additions & 0 deletions modules/os2forms_audit/src/Controller/LocalTasksController.php
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);
}

}
123 changes: 123 additions & 0 deletions modules/os2forms_audit/src/Form/PluginSettingsForm.php
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 ?? []);
}

}
28 changes: 22 additions & 6 deletions modules/os2forms_audit/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,27 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config(self::$configName);

$plugins = $this->loggerManager->getDefinitions();
ksort($plugins);
$options = array_map(function ($plugin) {
/** @var \Drupal\Core\StringTranslation\TranslatableMarkup $title */
$title = $plugin['title'];
return $title->render();
}, $plugins);

$form['enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enabled'),
'#description' => $this->t('E'),
'#default_value' => $config->get('enabled'),
$form['provider'] = [
'#type' => 'select',
'#title' => $this->t('Log provider'),
'#description' => $this->t('Select the logger provider you which to use'),
'#options' => $options,
'#default_value' => $config->get('provider'),
];

$form['fallback'] = [
'#type' => 'select',
'#title' => $this->t('Fallback Log provider'),
'#description' => $this->t('Select the logger provider you which to use, if the main provider fails'),
'#options' => $options,
'#default_value' => $config->get('fallback'),
];

return parent::buildForm($form, $form_state);
Expand All @@ -81,7 +96,8 @@ public function submitForm(array &$form, FormStateInterface $form_state): void {
parent::submitForm($form, $form_state);

$this->config(self::$configName)
->set('enabled', $form_state->getValue('enabled'))
->set('provider', $form_state->getValue('provider'))
->set('fallback', $form_state->getValue('fallback'))
->save();
}

Expand Down
51 changes: 49 additions & 2 deletions modules/os2forms_audit/src/Plugin/AuditLogger/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@

namespace Drupal\os2forms_audit\Plugin\AuditLogger;

use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\PluginFormInterface;

/**
* Writes entities to a file.
*
* @AuditLoggerProvider(
* id = "file",
* title = @Translation("File logger"),
* title = @Translation("File"),
* description = @Translation("Writes entities to a file.")
* )
*/
class File extends PluginBase implements AuditLoggerInterface {
class File extends PluginBase implements AuditLoggerInterface, PluginFormInterface, ConfigurableInterface{

public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
}

/**
* {@inheritdoc}
Expand All @@ -25,4 +33,43 @@ public function write(EntityInterface $entity): void {
file_put_contents('path_to_your_file.txt', serialize($entity));
}

public function getConfiguration(): array {
return $this->configuration;
}

public function setConfiguration(array $configuration): static {
$this->configuration = $configuration + $this->defaultConfiguration();
return $this;
}

public function defaultConfiguration(): array {
return [
'path' => '/tmp/os2forms_audit.log',
];
}

public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {

$form['path'] = [
'#type' => 'textfield',
'#title' => $this->t('Full path to the audit log file to store entries in'),
'#default_value' => $this->configuration['path'],
];

return $form;
}

public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// @todo Implement validateConfigurationForm() method.
}

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state->getErrors()) {
$values = $form_state->getValues();
$configuration = [
'path' => $values['path'],
];
$this->setConfiguration($configuration);
}
}
}
Loading

0 comments on commit 7b0ca43

Please sign in to comment.