gdpr_alert-1.x-dev/gdpr_alert.module

gdpr_alert.module
<?php

/**
 * @file
 * Contains gdpr_alert.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function gdpr_alert_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.gdpr_alert':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module allows you to display a GDPR consent alert bar.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function gdpr_alert_theme($existing, $type, $theme, $path) {
  return [
    'gdpr_alert' => [
      'variables' => [
        'alert' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_module_implements_alter().
 */
function gdpr_alert_module_implements_alter(&$implementations, $hook) {
  switch ($hook) {
    case 'page_top':
    case 'page_bottom':
      $group = $implementations['gdpr_alert'];
      unset($implementations['gdpr_alert']);
      $implementations['gdpr_alert'] = $group;
      break;
  }
}

/**
 * Implements hook_page_top().
 */
function gdpr_alert_page_top(array &$page_top) {
  $page_top['gdpr_alert'] = _gdpr_alert_build('top');
}

/**
 * Implements hook_page_bottom().
 */
function gdpr_alert_page_bottom(array &$page_bottom) {
  $page_bottom['gdpr_alert'] = _gdpr_alert_build('bottom');
}

/**
 * Build GDPR alert.
 *
 * @param string $position
 *
 * @return array
 */
function _gdpr_alert_build($position = 'top') {
  $build = [];

  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $state_keys = [
    'gdpr_alert_active',
    'gdpr_alert_position',
    'gdpr_alert_expiration',
    'gdpr_alert.' . $language . '.title',
    'gdpr_alert.' . $language . '.dismiss_title',
    'gdpr_alert.' . $language . '.dismiss',
    'gdpr_alert.' . $language . '.message',
    'gdpr_alert_key',
  ];

  $config = \Drupal::state()->getMultiple($state_keys);
  if ($config && !empty($config['gdpr_alert_active']) && $config['gdpr_alert_active'] && $config['gdpr_alert_position'] == $position) {

    /** @var \Drupal\Core\Routing\AdminContext $admin_context */
    $admin_context = \Drupal::service('router.admin_context');
    if (!$admin_context->isAdminRoute()) {

      $alert = [
        'title' => !empty($config['gdpr_alert.' . $language . '.title']) ? $config['gdpr_alert.' . $language . '.title'] : '',
        'message' => [
          '#type' => 'processed_text',
          '#text' => $config['gdpr_alert.' . $language . '.message']['value'] ?? '',
          '#format' => $config['gdpr_alert.' . $language . '.message']['format'] ?? NULL,
        ],
        'dismiss' => !empty($config['gdpr_alert.' . $language . '.dismiss']) ? $config['gdpr_alert.' . $language . '.dismiss'] : TRUE,
        'dismiss_title' => !empty($config['gdpr_alert.' . $language . '.dismiss_title']) ? $config['gdpr_alert.' . $language . '.dismiss_title'] : t('I Agree'),
      ];

      $build = [
        '#theme' => 'gdpr_alert',
        '#alert' => $alert,
      ];

      // Set up cookie expiration value for jQuery cookie function
      switch ($config['gdpr_alert_expiration']) {
        case 'year':
          $expiration = 365;
          break;
        case 'month':
          $expiration = 30;
          break;
        case 'week':
          $expiration = 7;
          break;
        case 'day':
          $expiration = 1;
          break;
        default:
          $expiration = 'default';
      }

      if (!empty($config['gdpr_alert_key'])) {
        $build['#attached'] = [
          'library' => ['gdpr_alert/alert_bar'],
          'drupalSettings' => [
            'gdpr_alert' => [
              'dismissedKey' => $config['gdpr_alert_key'],
              'cookieExpiration' => $expiration,
            ],
          ],
        ];
      }

      $cacheableMetadata = new CacheableMetadata();
      $cacheableMetadata->addCacheableDependency($config);
      $cacheableMetadata->addCacheTags(['gdpr_alert']);
      $cacheableMetadata->applyTo($build);
    }
  }

  return $build;
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function gdpr_alert_menu_local_tasks_alter(&$data, $route_name) {
  if ($route_name == 'gdpr_alert.alert_config_form' || $route_name == 'gdpr_alert.config_form') {
    $languageManager = \Drupal::languageManager();
    $current_language = $languageManager->getCurrentLanguage();
    $languages = $languageManager->getLanguages();
    foreach ($languages AS $language) {
      if ($language->getId() != $current_language->getId()) {
        $data['tabs'][0]['gdpr_alert.translate_' . $language->getId() . '_tab'] = [
          '#theme' => 'menu_local_task',
          '#link' => [
            'title' => t('Translate (%language)', ['%language' => $language->getName()]),
            'url' => Url::fromRoute('gdpr_alert.alert_config_form', [], [
              'language' => $language,
            ]),
            'localized_options' => [
              'attributes' => [
                'title' => t('Translate GDPR Alert'),
              ],
            ],
          ],
        ];
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc