You are here

class SettingsForm in CookieConsent 8

Class SettingsForm.

@package Drupal\cookieconsent\Form

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
cookieconsent.routing.yml in ./cookieconsent.routing.yml
cookieconsent.routing.yml

File

src/Form/SettingsForm.php, line 21
Contains Drupal\cookieconsent\Form\SettingsForm.

Namespace

Drupal\cookieconsent\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new SettingsForm object.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('entity_type.manager'));
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('cookieconsent.settings');
    $form['minified'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use the minified version (cookieconsent.min.js) of the Cookie Consent javascript plugin'),
      '#description' => $this
        ->t('If you want to be able to debug the javascript (by using cookieconsent.js), uncheck this box.'),
      '#default_value' => $config
        ->get('minified'),
    ];
    $form['theme'] = [
      '#type' => 'select',
      '#options' => [
        'none' => $this
          ->t('- None -'),
        'dark-top' => $this
          ->t('Dark Top'),
        'dark-floating' => $this
          ->t('Dark Floating'),
        'dark-bottom' => $this
          ->t('Dark Bottom'),
        'light-floating' => $this
          ->t('Light Floating'),
        'light-top' => $this
          ->t('Light Top'),
        'light-bottom' => $this
          ->t('Light Bottom'),
      ],
      '#title' => $this
        ->t('Choose your theme'),
      '#description' => $this
        ->t('Select the theme you wish to use.'),
      '#default_value' => $config
        ->get('theme'),
    ];
    $form['theme_path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Path to custom CSS file'),
      '#description' => $this
        ->t('Specify the path to the custom CSS file to use (e.g. <em>themes/[your-theme]/css/cookie-consent.css</em>). This custom CSS file will load only if NO theme is specified above.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('theme_path'),
    ];
    $form['texts'] = [
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#title' => $this
        ->t('Custom texts'),
    ];
    $form['texts']['customise'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Customise the text'),
      '#description' => $this
        ->t('Customise the text used on the cookie bar'),
      '#default_value' => $config
        ->get('customise'),
    ];
    $form['texts']['headline_text'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Headline Text'),
      '#description' => $this
        ->t('The message shown by the plugin.'),
      '#rows' => 2,
      '#default_value' => $config
        ->get('headline_text'),
      '#states' => [
        'visible' => [
          ':input[name="customise"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['texts']['accept_button_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Accept button text'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('accept_button_text'),
      '#states' => [
        'visible' => [
          ':input[name="customise"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['texts']['read_more_button_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Read more button text'),
      '#description' => $this
        ->t('The text shown on the link to the cookie policy (requires the Cookie policy option to also be set)'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('read_more_button_text'),
      '#states' => [
        'visible' => [
          ':input[name="customise"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['cookie_policy'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'node',
      '#title' => $this
        ->t('Your cookie policy'),
      '#description' => $this
        ->t('If you already have a cookie policy, link to it here.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('cookie_policy') ? $this->entityTypeManager
        ->getStorage('node')
        ->load($config
        ->get('cookie_policy')) : NULL,
    ];
    $form['container'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Container Element'),
      '#description' => $this
        ->t('The element you want the Cookie Consent notification to be appended to. When left empty, the Cookie Consent plugin is appended to the body.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('container'),
    ];
    $form['path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Path'),
      '#description' => $this
        ->t('The path for the consent cookie that Cookie Consent uses, to remember that users have consented to cookies. Use to limit consent to a specific path within your website.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('path'),
    ];
    $form['expiry'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Expiry days'),
      '#description' => $this
        ->t('The number of days Cookie Consent should store the user’s consent information for.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config
        ->get('expiry'),
    ];
    $form['target'] = [
      '#type' => 'select',
      '#options' => [
        '_blank' => t('_blank (a new window or tab)'),
        '_self' => t('_self (the same frame as it was clicked)'),
        '_parent' => t('_parent (the parent frame)'),
        '_top' => t('_top (the full body of the window)'),
      ],
      '#title' => $this
        ->t('Target'),
      '#description' => $this
        ->t('The <em>target</em> of the link to your cookie policy. Use to open a link in a new window, if you wish.'),
      '#default_value' => !empty($config
        ->get('target')) ? $config
        ->get('target') : '_self',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('cookieconsent.settings')
      ->set('minified', $form_state
      ->getValue('minified'))
      ->set('theme', $form_state
      ->getValue('theme'))
      ->set('theme_path', $form_state
      ->getValue('theme_path'))
      ->set('customise', $form_state
      ->getValue('customise'))
      ->set('headline_text', $form_state
      ->getValue('headline_text'))
      ->set('accept_button_text', $form_state
      ->getValue('accept_button_text'))
      ->set('read_more_button_text', $form_state
      ->getValue('read_more_button_text'))
      ->set('cookie_policy', $form_state
      ->getValue('cookie_policy'))
      ->set('container', $form_state
      ->getValue('container'))
      ->set('path', $form_state
      ->getValue('path'))
      ->set('expiry', $form_state
      ->getValue('expiry'))
      ->set('target', $form_state
      ->getValue('target'))
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsForm::$entityTypeManager protected property The entity type manager.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::__construct public function Constructs a new SettingsForm object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.