You are here

class SettingsFormWarning in General Data Protection Regulation Compliance 8

Implements the form controller.

Hierarchy

Expanded class hierarchy of SettingsFormWarning

1 string reference to 'SettingsFormWarning'
gdpr_compliance.routing.yml in ./gdpr_compliance.routing.yml
gdpr_compliance.routing.yml

File

src/Form/SettingsFormWarning.php, line 16

Namespace

Drupal\gdpr_compliance\Form
View source
class SettingsFormWarning extends ConfigFormBase {
  protected $moduleHandler;
  protected $entityTypeManager;
  protected $entityTypeBundle;
  protected $entityTypes;

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

  /**
   * SettingsFormWarning constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   Module handler service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity Manager service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfo $entityTypeManager
   *   Entity Manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $moduleHandler, EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfo $entityTypeBundle) {
    $this->moduleHandler = $moduleHandler;
    $this->entityTypeManager = $entityTypeManager;
    $this->entityTypeBundle = $entityTypeBundle;
    $this->entityTypes = [
      'contact_message' => [
        'name' => 'Contact',
        'module' => 'contact',
      ],
      'node' => [
        'name' => 'Node',
        'module' => 'node',
      ],
      'webform' => [
        'name' => 'Webform',
        'module' => 'webform',
      ],
    ];
    parent::__construct($config_factory);
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('gdpr_compliance.settings');
    $form['from-morelink'] = [
      '#title' => $this
        ->t('Url for form [Link to site policy agreement].'),
      '#type' => 'textfield',
      '#required' => TRUE,
      '#default_value' => $config
        ->get('from-morelink'),
      '#description' => $this
        ->t('Relative path starts with "/", or absolute start with http/https.'),
    ];
    $form['user'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('@module GDPR form warning', [
        '@module' => 'user',
      ]),
      '#open' => TRUE,
    ];
    $form["user"]['user-register'] = [
      '#title' => $this
        ->t('Enable on user registration'),
      '#description' => $this
        ->t('Display alert on user-register form (/user/register).'),
      '#type' => 'checkbox',
      '#default_value' => $config
        ->get('user-register'),
    ];
    $form["user"]['user-login'] = [
      '#title' => $this
        ->t('Enable on user login'),
      '#description' => $this
        ->t('Display alert on user-login form (/user/login).'),
      '#type' => 'checkbox',
      '#default_value' => $config
        ->get('user-login'),
    ];
    foreach ($this->entityTypes as $enity_type => $enity_info) {
      $name = $enity_info['name'];
      $form[$enity_type] = [
        '#type' => 'details',
        '#title' => $this
          ->t('@name GDPR form warning', [
          '@name' => $name,
        ]),
        '#open' => TRUE,
      ];
      if (!$this->moduleHandler
        ->moduleExists($enity_info['module'])) {
        $form[$enity_type]['#open'] = FALSE;
        $form[$enity_type]["{$enity_type}-miss"] = [
          '#markup' => '<p>' . $this
            ->t("Module '@module' not enabled.", [
            '@module' => $name,
          ]) . '</p>',
        ];
      }
      else {
        $form[$enity_type]["{$enity_type}-mode"] = [
          '#title' => $this
            ->t("Display mode"),
          '#type' => 'radios',
          '#options' => [
            'disable' => 'Disable',
            'all' => 'All',
            'custom' => 'Custom bundles',
          ],
          '#default_value' => $config
            ->get("{$enity_type}-mode"),
        ];
        $options = [];
        $bundles = $this
          ->getBundles($enity_type);
        if (!empty($bundles)) {
          foreach ($bundles as $key => $value) {
            $options[$key] = $value['label'];
          }
          $form[$enity_type]["{$enity_type}-bundles"] = [
            '#title' => $this
              ->t("@name bundles warning display on", [
              '@name' => $name,
            ]),
            '#type' => 'checkboxes',
            '#options' => $options,
          ];
          $default = $config
            ->get("{$enity_type}-bundles");
          if (!empty($default)) {
            $form[$enity_type]["{$enity_type}-bundles"]['#default_value'] = $default;
          }
        }
      }
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * Implements a form submit handler.
   */
  private function getBundles($enity_type) {
    if ($enity_type == 'webform') {
      $wforms = $this->entityTypeManager
        ->getStorage('webform')
        ->loadMultiple();
      $bundles = [];
      foreach ($wforms as $wform) {
        $bundles[$wform
          ->id()]['label'] = $wform
          ->label();
      }
    }
    else {
      $bundles = $this->entityTypeBundle
        ->getBundleInfo($enity_type);
    }
    return $bundles;
  }

  /**
   * Implements a form submit handler.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRebuild(TRUE);
    $config = $this
      ->config('gdpr_compliance.settings');
    foreach ($this->entityTypes as $enity_type => $enity_info) {
      $config
        ->set("{$enity_type}-mode", $form_state
        ->getValue("{$enity_type}-mode"))
        ->set("{$enity_type}-bundles", $form_state
        ->getValue("{$enity_type}-bundles"));
    }
    $config
      ->set('from-morelink', $form_state
      ->getValue('from-morelink'))
      ->set('user-login', $form_state
      ->getValue('user-login'))
      ->set('user-register', $form_state
      ->getValue('user-register'))
      ->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.
SettingsFormWarning::$entityTypeBundle protected property
SettingsFormWarning::$entityTypeManager protected property
SettingsFormWarning::$entityTypes protected property
SettingsFormWarning::$moduleHandler protected property
SettingsFormWarning::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsFormWarning::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsFormWarning::getBundles private function Implements a form submit handler.
SettingsFormWarning::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsFormWarning::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsFormWarning::submitForm public function Implements a form submit handler. Overrides ConfigFormBase::submitForm
SettingsFormWarning::__construct public function SettingsFormWarning constructor. 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.