Commit f21203dd authored by Andreas De Rijcke's avatar Andreas De Rijcke
Browse files

Issue #3313992 by andreasderijcke, fraserthompson: Support multilingual sites with multiple domains

parent 7c55e0cf
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -148,6 +148,8 @@ ADDITIONAL FEATURES
   disabled.
 * Status details on status report page. Checks availability of the configured
   scripts (incl. Auto-Blocking™ if enabled), warns if testing CDN is enabled.
 * Multi-domain support using URL language detection with domains, with all 
   module features scoped per domain (except for the IP bypass).


MAKING IT ALL WORK
+1 −0
Original line number Diff line number Diff line
@@ -20,3 +20,4 @@ gcm_deny_storages:
ip_whitelist: "20.54.106.120/29"
limit_to_paths: ""
test_cdn: false
override_language: ""
+7 −0
Original line number Diff line number Diff line
@@ -70,3 +70,10 @@ cookiepro_plus.config:
    test_cdn:
      type: boolean
      label: 'Enable Testing CDN'
    override_language:
      type: string
      label: 'Override language'

cookiepro_plus.config.*:
  type: cookiepro_plus.config
  label: 'CookiePro settings by domain'
+108 −52
Original line number Diff line number Diff line
@@ -5,10 +5,9 @@
 * Module (Un)Install hooks.
 */

use Drupal\Component\Render\FormattableMarkup;
use Drupal\cookiepro_plus\CookieProConfigKeysInterface;
use Drupal\Core\Link;
use Drupal\cookiepro_plus\CookieProConstantsInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;

/**
 * Implements hook_install().
@@ -26,68 +25,113 @@ function cookiepro_plus_requirements(string $phase): array {
    return [];
  }

  $requirements = [];

  /** @var \Drupal\cookiepro_plus\CookieProInterface $cookiepro */
  $cookiepro = \Drupal::service('cookiepro_plus');
  $domain_script = $cookiepro->getDomainScriptId();
  $testing = $cookiepro->hasTestCdnEnabled();
  $requirements = [];

  $config_link = Link::createFromRoute(new TranslatableMarkup('Configure'), 'cookiepro_plus.configuration', [], ['attributes' => ['target' => '_blank']]);
  $link = $config_link->toString();
  // Collect all the configs.
  $configs = [$cookiepro->getConfigDefault()];
  foreach ($cookiepro->getUrlLanguageNegotiationDomains() as $langcode => $domain) {

    $config = $cookiepro->getConfigOverride($langcode);
    if (!is_null($config)) {
      $configs[] = $config;
    }
  }

  // Check each config.
  foreach ($configs as $config) {

  $description = [];
    $errors = [];
  $value = [];
    $warnings = [];
    $status = [];
    $severity = REQUIREMENT_OK;

  if (!$domain_script) {
    $errors[] = new TranslatableMarkup('No script ID set. @configure.', ['@configure' => $link]);
    $description[] = new TranslatableMarkup('CookiePro will not work without a script ID.');
    if (empty($cookiepro->getDomainScriptId($config))) {
      $errors[] = new TranslatableMarkup('No script ID set');
    }
    else {
    $errors = $cookiepro->testScriptUrls();
      $errors = $cookiepro->testScriptUrls($config);
    }

  if (!empty($errors)) {
    $severity = REQUIREMENT_ERROR;

    $errors = implode('</li><li>', $errors);
    $errors = "<ol><li>$errors</li></ol>";
    if ($cookiepro->isPaused($config)) {
      $warnings[] = new TranslatableMarkup('Pause mode is enabled.');
    }

    $value[] = new TranslatableMarkup('Errors detected:');
    $value[] = $errors;
    if ($cookiepro->hasTestCdnEnabled($config)) {
      $warnings[] = new TranslatableMarkup('Testing CDN is enabled.');
    }
    else {
      $status[] = new TranslatableMarkup('Production CDN is enabled.');
    }

  if ($cookiepro->isPaused()) {
    $config_link->setText(new TranslatableMarkup('Disable'));
    $value[] = new TranslatableMarkup('CookiePro pause mode is enabled. @disable.', ['@disable' => $config_link->toString()]);
    $items = $status;
    if (!empty($warnings)) {

    if ($severity < REQUIREMENT_WARNING) {
      $severity = REQUIREMENT_WARNING;
      if (count($warnings) > 1) {
        $items[] = [
          '#theme' => 'item_list',
          '#prefix' => new TranslatableMarkup('Warnings:'),
          '#items' => $warnings,
        ];
      }
      else {
        $params = [
          '@warning' => reset($warnings),
        ];
        $items[] = new TranslatableMarkup('Warning: @warning', $params);
      }

  if ($testing) {
    $value[] = new TranslatableMarkup('Testing CDN is enabled. @configure.', ['@configure' => $link]);
    $description[] = new TranslatableMarkup('Testing CDN should be disabled on production environment.');

    if ($severity < REQUIREMENT_WARNING) {
      $severity = REQUIREMENT_WARNING;
    }

    if (!empty($errors)) {

      if (count($errors) > 1) {
        $items[] = [
          '#theme' => 'item_list',
          '#prefix' => new TranslatableMarkup('Errors:'),
          '#items' => $errors,
        ];
      }
      else {
        $params = [
          '@error' => reset($errors),
        ];
        $items[] = new TranslatableMarkup('Error: @error', $params);
      }
  elseif (empty($errors)) {
    $description[] = new TranslatableMarkup('Module config is OK for production.');

      $severity = REQUIREMENT_ERROR;
    }

  $value = (count($value) > 1) ? implode('<br/>', $value) : reset($value);
  $description = implode('<br>', $description);
  $requirements['cookiepro_plus'] = [
    $description = [
      '#theme' => 'item_list',
      '#items' => $items,
    ];

    $route = $cookiepro->getConfigurationRoute($config);
    $params = [
      ':url' => Url::fromRoute($route)->toString(),
    ];

    $label = $cookiepro->getLabel($config);
    if (!is_null($label)) {

      $params['%label'] = $label;
      $params['@label'] = $label;
      $value = new TranslatableMarkup("<a href=':url' title='Edit @label configuration' target='_blank'>%label</a> configuration status:", $params);
    }
    else {
      $value = new TranslatableMarkup("<a href=':url' title='Edit configuration' target='_blank'>Configuration</a> status:", $params);
    }

    $requirements[$config->getName()] = [
      'title' => 'CookiePro',
    'value' => new FormattableMarkup($value, []),
      'value' => $value,
      'description' => $description,
      'severity' => $severity,
    ];
  }

  return $requirements;
}
@@ -97,7 +141,19 @@ function cookiepro_plus_requirements(string $phase): array {
 */
function cookiepro_plus_uninstall(): void {

  \Drupal::service('config.factory')
    ->getEditable(CookieProConfigKeysInterface::CONFIG_KEY)
    ->delete();
  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');
  $config_factory->getEditable(CookieProConstantsInterface::CONFIG_KEY)->delete();

  // Just check if there is an override for each language, to keeps things easy.
  $languages = \Drupal::languageManager()->getLanguages();
  foreach ($languages as $language) {

    $config = $config_factory->getEditable(CookieProConstantsInterface::CONFIG_KEY . ".{$language->getId()}");
    // If the config does not exist, a new instance is returned.
    if (!$config->isNew()) {
      $config->delete();
    }
  }

}
+6 −1
Original line number Diff line number Diff line
cookiepro_plus.configuration:
  route_name: cookiepro_plus.configuration
  title: 'General'
  title: 'Default'
  base_route: cookiepro_plus.configuration
  weight: 0

cookiepro_plus.configuration.overrides:
  class: Drupal\Core\Menu\LocalTaskDefault
  deriver: Drupal\cookiepro_plus\Plugin\Derivative\CookieProLocalTask
Loading