You are here

eu_cookie_compliance.install in EU Cookie Compliance (GDPR Compliance) 8

Update scripts for the EU Cookie Compliance module.

File

eu_cookie_compliance.install
View source
<?php

/**
 * @file
 * Update scripts for the EU Cookie Compliance module.
 */
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\eu_cookie_compliance\Entity\CookieCategoryInterface;
use Drupal\user\Entity\Role;
use Drupal\Core\Database\Database;
use Drupal\filter\Entity\FilterFormat;

/**
 * Implements hook_schema().
 */
function eu_cookie_compliance_schema() {
  $schema['eu_cookie_compliance_basic_consent'] = [
    'description' => 'Basic consent storage for EU Cookie Compliance / GDPR.',
    'fields' => [
      'cid' => [
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique consent storage ID.',
      ],
      'uid' => [
        'description' => '{users}.uid for user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'timestamp' => [
        'description' => 'Time of consent.',
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'ip_address' => [
        'description' => 'The IP address.',
        'type' => 'varchar',
        // Maximum length of an ipv6 IP address.
        'length' => 45,
        'not null' => TRUE,
        'default' => '',
      ],
      'consent_type' => [
        'description' => 'The type of consent, such as "banner" for the banner and form_id for forms.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'revision_id' => [
        'description' => 'Revision of the privacy policy at the time of consent.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'cid',
    ],
    'indexes' => [
      'uid' => [
        'uid',
      ],
    ],
    'foreign keys' => [
      'uid' => [
        'users' => 'uid',
      ],
    ],
  ];
  return $schema;
}

/**
 * Implements hook_install().
 */
function eu_cookie_compliance_install() {
  module_load_include('module', 'eu_cookie_compliance', 'eu_cookie_compliance');
  $roles = Role::loadMultiple();
  $permission = 'display eu cookie compliance popup';
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, [
      $permission,
    ]);
  }
  if (Settings::get('eu_cookie_compliance_validate_filter_format', TRUE)) {

    // Allow individual installations to opt out of the filter format validation
    // behaviour.
    $all_filter_formats = filter_formats(\Drupal::currentUser());
    $default_filter_format = filter_fallback_format();
    if (count($all_filter_formats)) {

      // Determine the best default text format to use.
      $default_filter_format = filter_default_format();
      $full_html_format = FilterFormat::load('full_html');
      if (($default_filter_format === 'restricted_html' || $default_filter_format === 'plain_text') && $full_html_format !== NULL && $full_html_format
        ->get('status')) {
        $default_filter_format = 'full_html';
      }
    }
  }

  // We can't be sure "Restricted HTML" exists.
  // Initialize rich text editors with a text format that actually exists.
  // https://www.drupal.org/project/eu_cookie_compliance/issues/2979440 .
  $config = \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings');
  if (!$config
    ->get('uuid')) {
    $config
      ->set('uuid', \Drupal::service('uuid')
      ->generate());
  }
  $config
    ->set('popup_agreed.format', $default_filter_format)
    ->set('popup_info.format', $default_filter_format)
    ->set('mobile_popup_info.format', $default_filter_format)
    ->set('withdraw_message.format', $default_filter_format)
    ->save();
  eu_cookie_compliance_module_set_weight();
}

/**
 * Implements hook_requirements().
 */
function eu_cookie_compliance_requirements($phase) {
  $requirements = [];
  if ($phase === 'runtime') {
    $popup_link = Drupal::config('eu_cookie_compliance.settings')
      ->get('popup_link');
    $show_policy = Drupal::config('eu_cookie_compliance.settings')
      ->get('show_disagree_button');
    if ($popup_link === '<front>' && $show_policy) {
      $requirements['eu_cookie_compliance'] = [
        'title' => t('EU Cookie Compliance'),
        'severity' => REQUIREMENT_WARNING,
        'description' => 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.'),
        'value' => t('Privacy policy link not provided'),
      ];
    }
  }
  return $requirements;
}

/**
 * Force default value for "cookie_lifetime" item.
 */
function eu_cookie_compliance_update_8101() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('cookie_lifetime', 100)
    ->save();
}

/**
 * Reverse the setting for "Consent by clicking" (solving a module beta bug).
 */
function eu_cookie_compliance_update_8102() {
  $consent_by_clicking_value = \Drupal::configFactory()
    ->get('eu_cookie_compliance.settings')
    ->get('popup_clicking_confirmation');
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('popup_clicking_confirmation', !$consent_by_clicking_value)
    ->save();
}

/**
 * Fix bug with mobile banner message from beta 9.
 */
function eu_cookie_compliance_update_8103() {
  $mobile_popup_info = \Drupal::configFactory()
    ->get('eu_cookie_compliance.settings')
    ->get('mobile_popup_info');
  if (!is_array($mobile_popup_info) && $mobile_popup_info == '') {
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->set('mobile_popup_info', [
      'value' => '',
      'format' => filter_default_format(),
    ])
      ->save();
  }
}

/**
 * Change seconds to milliseconds for animation duration.
 */
function eu_cookie_compliance_update_8104() {
  $popup_delay = \Drupal::configFactory()
    ->get('eu_cookie_compliance.settings')
    ->get('popup_delay');
  if ($popup_delay < 10) {
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->set('popup_delay', $popup_delay * 1000)
      ->save();
  }
}

/**
 * Create new config value for show or hide the cookie policy button.
 */
function eu_cookie_compliance_update_8105() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('show_disagree_button', TRUE)
    ->save();
}

/**
 * Create new config value to handle consent options.
 */
function eu_cookie_compliance_update_8106() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('method', 'default')
    ->set('disagree_button_label', 'No, thanks')
    ->set('disabled_javascripts', '')
    ->set('whitelisted_cookies', '')
    ->save();
}

/**
 * Add table to handle basic consent.
 */
function eu_cookie_compliance_update_8107() {
  $schema['eu_cookie_compliance_basic_consent'] = [
    'description' => 'Basic consent storage for EU Cookie Compliance / GDPR.',
    'fields' => [
      'cid' => [
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique consent storage ID.',
      ],
      'uid' => [
        'description' => '{users}.uid for user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'timestamp' => [
        'description' => 'Time of consent.',
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'ip_address' => [
        'description' => 'The IP address.',
        'type' => 'varchar',
        // Maximum length of an ipv6 IP address.
        'length' => 45,
        'not null' => TRUE,
        'default' => '',
      ],
      'consent_type' => [
        'description' => 'The type of consent, such as "banner" for the banner and form_id for forms.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ],
      'revision_id' => [
        'description' => 'Revision of the privacy policy at the time of consent.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => [
      'cid',
    ],
    'indexes' => [
      'uid' => [
        'uid',
      ],
    ],
    'foreign keys' => [
      'uid' => [
        'users' => 'uid',
      ],
    ],
  ];
  if (!Database::getConnection()
    ->schema()
    ->tableExists('eu_cookie_compliance_basic_consent')) {
    Database::getConnection()
      ->schema()
      ->createTable('eu_cookie_compliance_basic_consent', $schema['eu_cookie_compliance_basic_consent']);
  }
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('consent_storage_method', 'do_not_store')
    ->save();
}

/**
 * Change module weight to load after other modules, ensure all JSs are handled.
 */
function eu_cookie_compliance_update_8108() {
  module_load_include('module', 'eu_cookie_compliance', 'eu_cookie_compliance');
  eu_cookie_compliance_module_set_weight();
}

/**
 * Add config variables for the withdraw banner.
 */
function eu_cookie_compliance_update_8109() {
  $config = \Drupal::config('eu_cookie_compliance.settings');
  $default_filter_format = filter_default_format();
  $full_html_format = FilterFormat::load('full_html');
  if (($default_filter_format === 'restricted_html' || $default_filter_format === 'plain_text') && !empty($full_html_format) && $full_html_format
    ->get('status')) {
    $default_filter_format = 'full_html';
  }
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('withdraw_message', [
    'value' => $config
      ->get('withdraw_message')['value'] ?? '<h2>We use cookies on this site to enhance your user experience</h2><p>You have given your consent for us to set cookies.</p>',
    'format' => $config
      ->get('withdraw_message')['format'] ?? $default_filter_format,
  ])
    ->set('withdraw_action_button_label', 'Withdraw consent')
    ->set('withdraw_tab_button_label', 'Privacy settings')
    ->set('withdraw_enabled', FALSE)
    ->set('popup_info_template', 'legacy')
    ->save();
}

/**
 * Disable withdraw tab and banner in the consent method "Consent by default".
 */
function eu_cookie_compliance_update_8110() {
  $withdraw_enabled = \Drupal::configFactory()
    ->get('eu_cookie_compliance.settings')
    ->get('withdraw_enabled');
  $method = \Drupal::configFactory()
    ->get('eu_cookie_compliance.settings')
    ->get('method');
  if ($method === 'default' && $withdraw_enabled == 1) {
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->set('withdraw_enabled', FALSE)
      ->save();
  }
}

/**
 * Adding config variable for the popup info template.
 */
function eu_cookie_compliance_update_8111() {
  $popup_info_template = \Drupal::configFactory()
    ->get('eu_cookie_compliance.settings')
    ->get('popup_info_template');
  if ($popup_info_template === NULL) {
    $method = \Drupal::configFactory()
      ->get('eu_cookie_compliance.settings')
      ->get('method');

    // Set popup_info_template value to 'new' if method is not 'consent by
    // default'.
    $popup_info_template = $method === 'default' ? 'legacy' : 'new';
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->set('popup_info_template', $popup_info_template)
      ->save();
  }
}

/**
 * Renaming variables show_disagree_button and popup_disagree_button_message.
 */
function eu_cookie_compliance_update_8115() {
  $show_disagree_button = \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->get('show_disagree_button');
  $popup_disagree_button_message = \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->get('popup_disagree_button_message');
  if (!empty($show_disagree_button)) {
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->set('show_more_info', $show_disagree_button)
      ->save();
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->clear('show_disagree_button')
      ->save();
  }
  if (!empty($popup_disagree_button_message)) {
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->set('popup_more_info_button_message', $popup_disagree_button_message)
      ->save();
    \Drupal::configFactory()
      ->getEditable('eu_cookie_compliance.settings')
      ->clear('popup_disagree_button_message')
      ->save();
  }
}

/**
 * Add config variables for the "Opt-in with categories" feature.
 */
function eu_cookie_compliance_update_8112() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('cookie_categories', '')
    ->set('enable_save_preferences_button', TRUE)
    ->set('save_preferences_button_label', 'Save preferences')
    ->set('accept_all_categories_button_label', 'Accept all cookies')
    ->set('fix_first_cookie_category', TRUE)
    ->set('select_all_categories_by_default', FALSE)
    ->set('withdraw_button_on_info_popup', FALSE)
    ->save();
}

/**
 * Add config variables to "Allow cookie for all sites on same domain" feature.
 */
function eu_cookie_compliance_update_8113() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('domain_all_sites', FALSE)
    ->save();
}

/**
 * Add uuid to the config.
 */
function eu_cookie_compliance_update_8114() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('uuid', \Drupal::service('uuid')
    ->generate())
    ->save();
}

/**
 * Add option for pages to be reloaded.
 */
function eu_cookie_compliance_update_8116() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('reload_options', 0)
    ->set('reload_routes_list', '')
    ->save();
}

/**
 * Add config variable for containing element.
 */
function eu_cookie_compliance_update_8117() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('containing_element', 'body')
    ->save();
}

/**
 * Add config variable for privacy settings tab.
 */
function eu_cookie_compliance_update_8118() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('settings_tab_enabled', FALSE)
    ->save();
}

/**
 * Add toggle for automatic cookies removal to the config.
 */
function eu_cookie_compliance_update_8119() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('automatic_cookies_removal', TRUE)
    ->save();
}

/**
 * Add config variable for setting cookie lifetime to zero on disagree.
 */
function eu_cookie_compliance_update_8120() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('set_cookie_session_zero_on_disagree', FALSE)
    ->save();
}

/**
 * Add config variable for cookie policy version.
 */
function eu_cookie_compliance_update_8121() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('cookie_policy_version', '1.0.0')
    ->save();
}

/**
 * Install new cookie category configuration entity.
 */
function eu_cookie_compliance_update_8122(&$sandbox) {
  $definition = [
    'id' => 'cookie_category',
    'label' => new TranslatableMarkup('Cookie category'),
    'label_collection' => new TranslatableMarkup('Cookie categories'),
    'handlers' => [
      'storage' => 'Drupal\\eu_cookie_compliance\\CategoryStorageManager',
      'view_builder' => 'Drupal\\Core\\Entity\\EntityViewBuilder',
      'list_builder' => 'Drupal\\eu_cookie_compliance\\CookieCategoryListBuilder',
      'form' => [
        'add' => 'Drupal\\eu_cookie_compliance\\Form\\CookieCategoryForm',
        'edit' => 'Drupal\\eu_cookie_compliance\\Form\\CookieCategoryForm',
        'delete' => 'Drupal\\eu_cookie_compliance\\Form\\CookieCategoryDeleteForm',
      ],
      'route_provider' => [
        'html' => 'Drupal\\eu_cookie_compliance\\CookieCategoryHtmlRouteProvider',
      ],
    ],
    'config_prefix' => 'cookie_category',
    'config_export' => [
      'id',
      'label',
      'description',
      'checkbox_default_state',
      'weight',
    ],
    'admin_permission' => 'administer eu cookie compliance categories',
    'entity_keys' => [
      'id' => 'id',
      'label' => 'label',
      'uuid' => 'uuid',
      'description' => 'description',
      'checkbox_default_state' => 'checkbox_default_state',
      'weight' => 'weight',
    ],
    'links' => [
      'add-form' => '/admin/config/system/eu-cookie-compliance/categories/add',
      'edit-form' => '/admin/config/system/eu-cookie-compliance/categories/{cookie_category}/edit',
      'delete-form' => '/admin/config/system/eu-cookie-compliance/categories/{cookie_category}/delete',
      'collection' => '/admin/config/system/eu-cookie-compliance/categories',
    ],
  ];
  $entity_type = new ConfigEntityType($definition);
  \Drupal::entityDefinitionUpdateManager()
    ->installEntityType($entity_type);
}

/**
 * Move categories into standalone config entities.
 */
function eu_cookie_compliance_update_8123(&$sandbox) {

  // Load current categories from config.
  $config = Drupal::config('eu_cookie_compliance.settings');
  $cookie_categories = $config
    ->get('cookie_categories');
  $cookie_categories = _eu_cookie_compliance_extract_category_key_label_description($cookie_categories);
  $fix_first_cookie_category = $config
    ->get('fix_first_cookie_category');
  $select_all_categories_by_default = $config
    ->get('select_all_categories_by_default');
  $languages = Drupal::languageManager()
    ->getLanguages();

  /** @var \Drupal\eu_cookie_compliance\CategoryStorageManager $storage */
  $storage = Drupal::entityTypeManager()
    ->getStorage('cookie_category');
  $i = 0;

  // Transform categories into config entities.
  foreach ($cookie_categories as $machine_name => $category) {
    $checkbox_default_state = 'unchecked';

    // Preserve all categories being automatically checked
    // and first category's special treatment, if any.
    if ($select_all_categories_by_default) {
      $checkbox_default_state = 'checked';
    }
    if ($i === 0 && $fix_first_cookie_category) {
      $checkbox_default_state = 'required';
    }
    if (!$storage
      ->load($machine_name) instanceof CookieCategoryInterface) {
      $data = [
        'id' => $machine_name,
        'label' => $category['label'],
        'description' => isset($category['description']) ? $category['description'] : '',
        'checkbox_default_state' => $checkbox_default_state,
        'weight' => $i++,
      ];
      $storage
        ->create($data)
        ->save();
    }
  }

  // Migrate translations as well.
  if (\Drupal::languageManager()
    ->isMultilingual()) {
    foreach ($languages as $langcode => $language) {
      $config_translation = \Drupal::languageManager()
        ->getLanguageConfigOverride($langcode, 'eu_cookie_compliance.settings');
      $cookie_categories = $config_translation
        ->get('cookie_categories');
      $cookie_categories = _eu_cookie_compliance_extract_category_key_label_description($cookie_categories);

      /** @var \Drupal\eu_cookie_compliance\CategoryStorageManager $storage */
      $storage = Drupal::entityTypeManager()
        ->getStorage('cookie_category');
      foreach ($cookie_categories as $machine_name => $category) {
        $category_entity = $storage
          ->load($machine_name);
        if ($category_entity) {
          $category_entity_translation = \Drupal::languageManager()
            ->getLanguageConfigOverride($langcode, $category_entity
            ->getConfigDependencyName());
          $category_entity_translation
            ->set('label', $category['label']);
          $category_entity_translation
            ->set('description', isset($category['description']) ? $category['description'] : '');
          $category_entity_translation
            ->save();
        }
      }
    }
  }

  // Clear old values from original configuration.
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->clear('cookie_categories')
    ->clear('fix_first_cookie_category')
    ->clear('select_all_categories_by_default')
    ->save();
}

/**
 * Add config variables for cookie values.
 */
function eu_cookie_compliance_update_8124() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('cookie_value_disagreed', '0')
    ->set('cookie_value_agreed_show_thank_you', '1')
    ->set('cookie_value_agreed', '2')
    ->save();
}

/**
 * Add config variables for button accessibility.
 */
function eu_cookie_compliance_update_8125() {
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->set('accessibility_focus', FALSE)
    ->save();
}

Functions

Namesort descending Description
eu_cookie_compliance_install Implements hook_install().
eu_cookie_compliance_requirements Implements hook_requirements().
eu_cookie_compliance_schema Implements hook_schema().
eu_cookie_compliance_update_8101 Force default value for "cookie_lifetime" item.
eu_cookie_compliance_update_8102 Reverse the setting for "Consent by clicking" (solving a module beta bug).
eu_cookie_compliance_update_8103 Fix bug with mobile banner message from beta 9.
eu_cookie_compliance_update_8104 Change seconds to milliseconds for animation duration.
eu_cookie_compliance_update_8105 Create new config value for show or hide the cookie policy button.
eu_cookie_compliance_update_8106 Create new config value to handle consent options.
eu_cookie_compliance_update_8107 Add table to handle basic consent.
eu_cookie_compliance_update_8108 Change module weight to load after other modules, ensure all JSs are handled.
eu_cookie_compliance_update_8109 Add config variables for the withdraw banner.
eu_cookie_compliance_update_8110 Disable withdraw tab and banner in the consent method "Consent by default".
eu_cookie_compliance_update_8111 Adding config variable for the popup info template.
eu_cookie_compliance_update_8112 Add config variables for the "Opt-in with categories" feature.
eu_cookie_compliance_update_8113 Add config variables to "Allow cookie for all sites on same domain" feature.
eu_cookie_compliance_update_8114 Add uuid to the config.
eu_cookie_compliance_update_8115 Renaming variables show_disagree_button and popup_disagree_button_message.
eu_cookie_compliance_update_8116 Add option for pages to be reloaded.
eu_cookie_compliance_update_8117 Add config variable for containing element.
eu_cookie_compliance_update_8118 Add config variable for privacy settings tab.
eu_cookie_compliance_update_8119 Add toggle for automatic cookies removal to the config.
eu_cookie_compliance_update_8120 Add config variable for setting cookie lifetime to zero on disagree.
eu_cookie_compliance_update_8121 Add config variable for cookie policy version.
eu_cookie_compliance_update_8122 Install new cookie category configuration entity.
eu_cookie_compliance_update_8123 Move categories into standalone config entities.
eu_cookie_compliance_update_8124 Add config variables for cookie values.
eu_cookie_compliance_update_8125 Add config variables for button accessibility.