cookie_blocking_libraries-1.0.x-dev/src/Form/ScriptCategorisationForm.php
src/Form/ScriptCategorisationForm.php
<?php namespace Drupal\cookie_blocking_libraries\Form; use Drupal\cookie_blocking_libraries\CookieImplementationInterface; use Drupal\cookie_blocking_libraries\CookieImplementationPluginManager; use Drupal\cookie_blocking_libraries\LibrariesServiceInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; /** * Configure script categorisation settings. */ class ScriptCategorisationForm extends ConfigFormBase { /** * The libraries service. * * @var \Drupal\cookie_blocking_libraries\LibrariesServiceInterface */ protected LibrariesServiceInterface $librariesService; /** * The plugin manager. * * @var \Drupal\cookie_blocking_libraries\CookieImplementationPluginManager */ protected CookieImplementationPluginManager $pluginManager; /** * The enabled plugin. * * @var \Drupal\cookie_blocking_libraries\CookieImplementationInterface|null */ protected CookieImplementationInterface|null $enabledPlugin; /** * {@inheritDoc} */ public function __construct(ConfigFactoryInterface $config_factory) { parent::__construct($config_factory); $this->librariesService = \Drupal::service('cookie_blocking_libraries.libraries_service'); $this->pluginManager = \Drupal::service('plugin.manager.cookie_blocking_libraries_implementation'); } /** * {@inheritdoc} */ public function getFormId() { return 'cookie_blocking_libraries_categorisation_settings'; } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return ['cookie_blocking_libraries.categorisation_settings']; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $this->enabledPlugin = $this->pluginManager->getEnabledPlugin(); if (empty($this->enabledPlugin)) { $form['choose_plugin'] = [ '#type' => 'link', '#title' => $this->t('No cookie implementation plugin has been selected. Click here to choose an implementation plugin'), '#weight' => 0, '#url' => Url::fromRoute('cookie_blocking_libraries.cookie_implementation_settings'), ]; return $form; } \Drupal::messenger()->addMessage($this->t('IMPORTANT - cookie blocking and unblocking will not work with aggregated scripts. An alternative method will need to be used for aggregation.')); $libraries = $this->librariesService->getAllLibraries(); $this->addTable($form, 'selections', $this->t('Libraries'), $libraries); $page_attachments = $this->librariesService->getPageAttachments(); $this->addTable($form, 'attachments', $this->t('HTML attachments'), $page_attachments); return parent::buildForm($form, $form_state); } /** * Add a Form API table element. * * @param array $form * The Form API array. * @param string $key * The Form API key. * @param string $caption * The table caption. * @param array $rows * The rows. */ protected function addTable(array &$form, $key, $caption, array $rows) { $headers = [ 'extension' => $this->t('Module'), 'type' => $this->t('Cookie type'), 'identifiers' => '', ]; $cookie_types = $this->enabledPlugin->getCategories(); $form[$key] = [ '#type' => 'table', '#caption' => $caption, '#header' => $headers, ]; $selections = $this->config('cookie_blocking_libraries.categorisation_settings')->get($key) ?? []; foreach ($rows as $extension => $data) { $form[$key][$extension] = [ 'extension' => [ '#markup' => $extension, ], 'type' => [ '#type' => 'select', '#options' => $cookie_types, '#default_value' => $selections[$extension]['type'] ?? $this->enabledPlugin->getDefaultCategory(), ], ]; $form[$key][$extension]['identifiers'] = [ '#type' => 'hidden', '#value' => isset($data['identifiers']) ? implode('|', $data['identifiers']) : '', ]; } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('cookie_blocking_libraries.categorisation_settings') ->set('selections', $form_state->getValue('selections')) ->set('attachments', $form_state->getValue('attachments')) ->save(); parent::submitForm($form, $form_state); $this->librariesService->flushLibraries(); } }