You are here

public function EuCookieComplianceConfigForm::submitForm in EU Cookie Compliance (GDPR Compliance) 8

Same name and namespace in other branches
  1. 2.0.x src/Form/EuCookieComplianceConfigForm.php \Drupal\eu_cookie_compliance\Form\EuCookieComplianceConfigForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/EuCookieComplianceConfigForm.php, line 1062

Class

EuCookieComplianceConfigForm
Provides settings for eu_cookie_compliance module.

Namespace

Drupal\eu_cookie_compliance\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Clear values if we are using minimal css.
  if ($form_state
    ->getValue('use_bare_css')) {
    $form_state
      ->setValue('popup_bg_hex', '');
    $form_state
      ->setValue('popup_text_hex', '');
    $form_state
      ->setValue('popup_height', '');
    $form_state
      ->setValue('popup_width', '');
  }

  // If there's no mobile message entered, disable the feature.
  if (trim($form_state
    ->getValue('mobile_popup_info')['value']) == '') {
    $form_state
      ->setValue('use_mobile_message', FALSE);
  }
  if ($form_state
    ->getValue('popup_link') === '<front>' && $form_state
    ->getValue('show_more_info')) {
    $this
      ->messenger()
      ->addError($this
      ->t('Your privacy policy link is pointing at the front page. This is the default value after installation, and unless your privacy policy is actually posted at the front page, you will need to create a separate page for the privacy policy and link to that page.'));
  }

  // Save permissions.
  $permission_name = 'display eu cookie compliance popup';
  $readonly_enabled = $this->moduleHandler
    ->moduleExists('config_readonly') && Settings::get('config_readonly');
  $allowlist_patterns = Settings::get('config_readonly_whitelist_patterns');
  foreach ($this
    ->getRoles() as $role_name => $role) {

    /** @var \Drupal\user\Entity\Role $role */
    if (!$role
      ->isAdmin()) {
      $user_settings_allowlisted = !empty($allowlist_patterns) && in_array('user.role.' . $role_name, $allowlist_patterns);
      if (!$readonly_enabled || $readonly_enabled && $user_settings_allowlisted) {
        if (array_key_exists($role_name, $form_state
          ->getValue('see_the_banner')) && $form_state
          ->getValue('see_the_banner')[$role_name]) {
          user_role_grant_permissions($role_name, [
            $permission_name,
          ]);
        }
        else {
          user_role_revoke_permissions($role_name, [
            $permission_name,
          ]);
        }
      }
      else {
        $this
          ->messenger()
          ->addWarning($this
          ->t('The user role permission could not be set because the <code>config_readonly</code> module is enabled. To allow setting of user role permissions, please add <code>user.role.%name</code> to the <code>config_readonly</code> allowlist.', [
          '%name' => $role_name,
        ]));
      }
    }
  }

  // Handle legacy settings for popup_position:
  if ($form_state
    ->getValue('popup_position') === 'top') {
    $form_state
      ->setValue('popup_position', TRUE);
  }
  elseif ($form_state
    ->getValue('popup_position') === 'bottom') {
    $form_state
      ->setValue('popup_position', FALSE);
  }
  $method = $form_state
    ->getValue('method');
  if ($method !== 'default') {
    $form_state
      ->setValue('popup_clicking_confirmation', FALSE);
    $form_state
      ->setValue('popup_scrolling_confirmation', FALSE);
    $form_state
      ->setValue('popup_info_template', 'new');
  }
  else {
    $form_state
      ->setValue('allowed_cookies', '');
    $form_state
      ->setValue('disabled_javascripts', '');
    $form_state
      ->setValue('withdraw_enabled', FALSE);
    $form_state
      ->setValue('settings_tab_enabled', FALSE);
  }

  // Clear cached javascript.
  Cache::invalidateTags([
    'library_info',
  ]);
  $extension_settings_allowlisted = !empty($allowlist_patterns) && in_array('core.extension' . $role_name, $allowlist_patterns);
  if (!$readonly_enabled || $readonly_enabled && $extension_settings_allowlisted) {
    eu_cookie_compliance_module_set_weight();
  }
  else {
    $this
      ->messenger()
      ->addWarning($this
      ->t('The module weight could not be set because the <code>config_readonly</code> module is enabled. To allow setting of module weight, please add <code>core.extension</code> to the <code>config_readonly</code> allowlist.'));
  }

  // Handle version 2 of jQuery colorpicker.
  $jquery_colorpicker_version = _eu_cookie_compliance_get_jquery_colorpicker_version();
  $bg_color = $jquery_colorpicker_version === 2 ? substr($form_state
    ->getValue('popup_bg_hex'), 1) : $form_state
    ->getValue('popup_bg_hex');
  $text_color = $jquery_colorpicker_version === 2 ? substr($form_state
    ->getValue('popup_text_hex'), 1) : $form_state
    ->getValue('popup_text_hex');

  // Save settings.
  $this
    ->config('eu_cookie_compliance.settings')
    ->set('cookie_policy_version', $form_state
    ->getValue('cookie_policy_version'))
    ->set('domain', $form_state
    ->getValue('domain'))
    ->set('popup_enabled', $form_state
    ->getValue('popup_enabled'))
    ->set('popup_clicking_confirmation', $form_state
    ->getValue('popup_clicking_confirmation'))
    ->set('popup_scrolling_confirmation', $form_state
    ->getValue('popup_scrolling_confirmation'))
    ->set('popup_position', $form_state
    ->getValue('popup_position'))
    ->set('popup_agree_button_message', $form_state
    ->getValue('popup_agree_button_message'))
    ->set('show_more_info', $form_state
    ->getValue('show_more_info'))
    ->set('popup_more_info_button_message', $form_state
    ->getValue('popup_more_info_button_message'))
    ->set('popup_info', $form_state
    ->getValue('popup_info'))
    ->set('popup_info_template', $form_state
    ->getValue('popup_info_template'))
    ->set('use_mobile_message', $form_state
    ->getValue('use_mobile_message'))
    ->set('mobile_popup_info', $form_state
    ->getValue('use_mobile_message') ? $form_state
    ->getValue('mobile_popup_info') : [
    'value' => '',
    'format' => filter_default_format(),
  ])
    ->set('mobile_breakpoint', $form_state
    ->getValue('mobile_breakpoint'))
    ->set('popup_agreed_enabled', $form_state
    ->getValue('popup_agreed_enabled'))
    ->set('popup_hide_agreed', $form_state
    ->getValue('popup_hide_agreed'))
    ->set('popup_find_more_button_message', $form_state
    ->getValue('popup_find_more_button_message'))
    ->set('popup_hide_button_message', $form_state
    ->getValue('popup_hide_button_message'))
    ->set('popup_agreed', $form_state
    ->getValue('popup_agreed'))
    ->set('popup_link', $form_state
    ->getValue('popup_link'))
    ->set('popup_link_new_window', $form_state
    ->getValue('popup_link_new_window'))
    ->set('popup_height', $form_state
    ->getValue('popup_height'))
    ->set('popup_width', $form_state
    ->getValue('popup_width'))
    ->set('popup_delay', $form_state
    ->getValue('popup_delay'))
    ->set('popup_bg_hex', $bg_color)
    ->set('popup_text_hex', $text_color)
    ->set('domains_option', $form_state
    ->getValue('domains_option'))
    ->set('domains_list', $form_state
    ->getValue('domains_list'))
    ->set('exclude_paths', $form_state
    ->getValue('exclude_paths'))
    ->set('exclude_admin_theme', $form_state
    ->getValue('exclude_admin_theme'))
    ->set('cookie_lifetime', $form_state
    ->getValue('cookie_lifetime'))
    ->set('cookie_session', $form_state
    ->getValue('cookie_session'))
    ->set('set_cookie_session_zero_on_disagree', $form_state
    ->getValue('set_cookie_session_zero_on_disagree'))
    ->set('eu_only', $form_state
    ->getValue('eu_only'))
    ->set('eu_only_js', $form_state
    ->getValue('eu_only_js'))
    ->set('use_bare_css', $form_state
    ->getValue('use_bare_css'))
    ->set('disagree_do_not_show_popup', $form_state
    ->getValue('disagree_do_not_show_popup'))
    ->set('reload_page', $form_state
    ->getValue('reload_page'))
    ->set('reload_options', $form_state
    ->getValue('reload_options'))
    ->set('reload_routes_list', $form_state
    ->getValue('reload_routes_list'))
    ->set('domain_all_sites', $form_state
    ->getValue('domain_all_sites'))
    ->set('cookie_name', $form_state
    ->getValue('cookie_name'))
    ->set('cookie_value_disagreed', $form_state
    ->getValue('cookie_value_disagreed'))
    ->set('cookie_value_agreed_show_thank_you', $form_state
    ->getValue('cookie_value_agreed_show_thank_you'))
    ->set('cookie_value_agreed', $form_state
    ->getValue('cookie_value_agreed'))
    ->set('exclude_uid_1', $form_state
    ->getValue('exclude_uid_1'))
    ->set('better_support_for_screen_readers', $form_state
    ->getValue('better_support_for_screen_readers'))
    ->set('fixed_top_position', $form_state
    ->getValue('fixed_top_position'))
    ->set('method', $form_state
    ->getValue('method'))
    ->set('disagree_button_label', $form_state
    ->getValue('disagree_button_label'))
    ->set('automatic_cookies_removal', $form_state
    ->getValue('automatic_cookies_removal'))
    ->set('allowed_cookies', $form_state
    ->getValue('allowed_cookies'))
    ->set('disabled_javascripts', $form_state
    ->getValue('disabled_javascripts'))
    ->set('consent_storage_method', $form_state
    ->getValue('consent_storage_method'))
    ->set('withdraw_message', $form_state
    ->getValue('withdraw_message'))
    ->set('withdraw_action_button_label', $form_state
    ->getValue('withdraw_action_button_label'))
    ->set('withdraw_tab_button_label', $form_state
    ->getValue('withdraw_tab_button_label'))
    ->set('withdraw_enabled', $form_state
    ->getValue('withdraw_enabled'))
    ->set('withdraw_button_on_info_popup', $form_state
    ->getValue('withdraw_button_on_info_popup'))
    ->set('enable_save_preferences_button', $form_state
    ->getValue('enable_save_preferences_button'))
    ->set('save_preferences_button_label', $form_state
    ->getValue('save_preferences_button_label'))
    ->set('accept_all_categories_button_label', $form_state
    ->getValue('accept_all_categories_button_label'))
    ->set('containing_element', $form_state
    ->getValue('containing_element'))
    ->set('settings_tab_enabled', $form_state
    ->getValue('settings_tab_enabled'))
    ->set('accessibility_focus', $form_state
    ->getValue('accessibility_focus'))
    ->save();
  parent::submitForm($form, $form_state);
}