iframe_consent-1.0.x-dev/src/Form/SettingsForm.php

src/Form/SettingsForm.php
<?php

namespace Drupal\iframe_consent\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure iframe_consent settings for this site.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * The iframe consent settings service.
   *
   * @var \Drupal\iframe_consent\Service\IframeConsentHelper
   */
  protected $iframeConsentHelper;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->iframeConsentHelper = $container->get('iframe_consent.helper');
    $instance->moduleHandler = $container->get('module_handler');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'iframe_consent_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['iframe_consent.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('iframe_consent.settings');

    $form['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable Iframe Consent'),
      '#description' => $this->t('Uncheck this box to temporarily disable Iframe Consent functionality.'),
      '#default_value' => $config->get('enabled'),
    ];

    $form['provider'] = [
      '#type' => 'select',
      '#required' => TRUE,
      '#title' => $this->t('Cookie compliance banner'),
      '#description' => $this->t('The system that provides the cookie compliance features.'),
      '#default_value' => $config->get('provider') ?? '',
      '#options' => $this->getProviders(),
    ];

    $form['manage_button_label'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Manage button label'),
      '#description' => $this->t('Label for the manage button.'),
      '#default_value' => $config->get('manage_button_label') ?? '',
    ];

    $form['accept_button_label'] = [
      '#type' => 'textfield',
      '#required' => TRUE,
      '#title' => $this->t('Accept button label'),
      '#description' => $this->t('Label for the accept button.'),
      '#default_value' => $config->get('accept_button_label') ?? '',
    ];

    $form['display_accept_button'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Display the accept button'),
      '#default_value' => $config->get('display_accept_button') ?? TRUE,
    ];

    $form['body'] = [
      '#type' => 'text_format',
      '#required' => TRUE,
      '#title' => $this->t('Body'),
      '#description' => $this->t('The body text for the iframe consent placeholder.'),
      '#default_value' => $config->get('body')['value'] ?? '',
      '#format' => $config->get('body')['format'] ?? '',
    ];

    $form['advanced_settings'] = [
      '#type' => 'details',
      '#title' => $this->t('Advanced settings'),
      '#open' => FALSE,
    ];

    $form['advanced_settings']['templates_by_domain'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable templates by domain'),
      '#description' => $this->t('Generate custom template placeholders for each domain.'),
      '#default_value' => $config->get('templates_by_domain') ?? FALSE,
    ];

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('iframe_consent.settings')
      ->set('enabled', $form_state->getValue('enabled'))
      ->set('provider', $form_state->getValue('provider'))
      ->set('manage_button_label', $form_state->getValue('manage_button_label'))
      ->set('accept_button_label', $form_state->getValue('accept_button_label'))
      ->set('display_accept_button', $form_state->getValue('display_accept_button'))
      ->set('body', $form_state->getValue('body'))
      ->set('templates_by_domain', $form_state->getValue('templates_by_domain'))
      ->save();

    $this->messenger()->addStatus($this->t('The configuration options have been saved.'));

    // Clear the cache tags.
    $this->iframeConsentHelper->clearCacheTags();
  }

  /**
   * Generate the list of providers.
   *
   * @return array
   *   An associative array of providers.
   */
  private function getProviders() {
    $providers = [
      '' => $this->t('Select'),
      'onetrust' => $this->t('OneTrust'),
    ];

    if ($this->moduleHandler->moduleExists('eu_cookie_compliance')) {
      $providers['eu_cookie_compliance'] = $this->t('EU Cookie Compliance');
    }

    return $providers;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc