You are here

function eu_cookie_compliance_update_7001 in EU Cookie Compliance (GDPR Compliance) 7

Same name and namespace in other branches
  1. 7.2 eu_cookie_compliance.install \eu_cookie_compliance_update_7001()

Migrate translation handling to Variable API.

NOTE: Make sure the variable_realm and i18n_variable modules are either enabled or in the filesystem if you are using multiple languages.

File

./eu_cookie_compliance.install, line 260
Installation file.

Code

function eu_cookie_compliance_update_7001(&$sandbox) {
  $languages = language_list();
  $variable_names = db_query("SELECT name FROM {variable} WHERE name LIKE '%eu_cookie_compliance_%' AND name NOT IN('eu_cookie_compliance_domain', 'eu_cookie_compliance_cookie_lifetime')")
    ->fetchCol();
  $default_value = array();
  if (count($variable_names) === 1) {

    // We only have one language.
    $name = reset($variable_names);
    $default_value = variable_get($name);
    variable_del($name);
  }
  else {

    // Multiple languages, need to install variable_realm module.
    $variable_exists = TRUE;
    if (!module_exists('i18n_variable')) {
      $variable_exists = module_enable(array(
        'i18n_variable',
      ));
    }
    if ($variable_exists) {
      $default_language = language_default('language');
      foreach ($variable_names as $name) {
        $langcode = str_replace('eu_cookie_compliance_', '', $name);
        if (isset($languages[$langcode])) {
          $value = variable_get($name);
          if ($langcode == $default_language) {
            $default_value = $value;
          }
          variable_realm_set('language', $langcode, 'eu_cookie_compliance', $value, FALSE);
          variable_del($name);
        }
      }

      // Enable translation variables for EU Cookie Compliance.
      $controller = variable_realm_controller('language');
      $old_variables = $controller
        ->getEnabledVariables();
      $old_list = variable_children($old_variables);
      $variables = array_merge($old_list, array(
        'eu_cookie_compliance',
      ));
      $controller
        ->setRealmVariable('list', $variables);
    }
    else {
      throw new DrupalUpdateException('EU Cookie Compliance now requires the modules variable ( https://www.drupal.org/project/variable ) and i18n ( https://www.drupal.org/project/i18n ) for multilingual sites. Please add these modules to your site.');
    }
  }

  // Even with i18n_variable, aan entry in the variable table is required.
  variable_set('eu_cookie_compliance', $default_value);
  cache_clear_all('variables', 'cache');
}