You are here

function eu_cookie_compliance_update_8123 in EU Cookie Compliance (GDPR Compliance) 8

Move categories into standalone config entities.

File

./eu_cookie_compliance.install, line 549
Update scripts for the EU Cookie Compliance module.

Code

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();
}