cookies_addons-1.0.3/modules/cookies_addons_views/cookies_addons_views.module

modules/cookies_addons_views/cookies_addons_views.module
<?php

/**
 * @file
 * Primary module hooks for Cookies Addons Views module.
 */

/**
 * Implements hook_views_pre_render().
 */
function cookies_addons_views_views_pre_render($view) {
  $id = $view->storage->id();
  $display_id = $view->current_display;
  if (_cookies_addons_views_is_restricted($id, $display_id)) {
    // Do not attach ajax settings to avoid issues
    // with duplicated drupalSettings.
    unset($view->element['#attached']['drupalSettings']['views']);

    if (isset($view->element['#viewsreference'])) {
      // Prevent viewsreference from adding problematic settings
      // by removing the viewsreference marker that triggers its hook.
      unset($view->element['#viewsreference']);
    }
  }
  return $view;
}

/**
 * Implements hook_module_implements_alter().
 */
function cookies_addons_views_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'views_pre_render') {
    // Execute cookies_addons_views_views_pre_render  at the end.
    $implementation = $implementations['cookies_addons_views'];
    unset($implementations['cookies_addons_views']);
    $implementations['cookies_addons_views'] = $implementation;
  }
}

/**
 * Implements template_preprocess_views_view().
 */
function cookies_addons_views_preprocess_views_view(&$variables) {
  $entityTypeManager = \Drupal::service('entity_type.manager');

  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];

  $id = $view->storage->id();
  $display_id = $view->current_display;
  $arguments = $view->args;

  if ($service = _cookies_addons_views_is_restricted($id, $display_id)) {
    $cookiesServices = $entityTypeManager
      ->getStorage('cookies_service')
      ->loadByProperties(['status' => 1]);
    if (isset($cookiesServices[$service])) {
      $serviceLabel = $cookiesServices[$service]->label();
    }
    else {
      $serviceLabel = $service;
    }

    // Preserve the original theme hook value (if it exists)
    $theme_hook_original = $variables['theme_hook_original'] ?? '';
    // Replace entire view with a placeholder.
    $variables = [];
    $variables['view'] = $view;
    $variables['rows'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'class' => 'cookies-addons-views-placeholder',
        'cookies-service' => $service,
        'service-name' => $serviceLabel,
        'view-id' => $id,
        'display-id' => $display_id,
        'data-args' => (is_array($arguments) && !empty($arguments)) ? implode('+', $arguments) : NULL,
      ],
      '#attached' => [
        'library' => 'cookies_addons_views/cookies-addons-views',
      ],
    ];
    // Restore the theme_hook_original value.
    $variables['theme_hook_original'] = $theme_hook_original;
  }
}

/**
 * Returns cookies service for restricted views.
 *
 * @param string $view_id
 *   View ID.
 * @param string $view_display_id
 *   View display ID.
 *
 * @return false|string
 *   Returns cookies service if applicable or FALSE.
 */
function _cookies_addons_views_is_restricted(string $view_id, string $view_display_id) {
  if (\Drupal::request()->getMethod() === 'POST') {
    return FALSE;
  }

  // Do nothing if there are no configurations.
  $restricted_views = \Drupal::config('cookies_addons_views.settings')
    ->get('views');
  if (!$restricted_views) {
    return FALSE;
  }

  // Gets views restriction from the settings.
  $restricted_views = preg_split('/\r\n|\r|\n/', $restricted_views);
  // Retrieves data about accepted cookies.
  $cookiesJson = \Drupal::request()->cookies->get('cookiesjsr');
  $cookies = [];

  if (is_string($cookiesJson) && $cookiesJson !== '') {
    $cookies = json_decode($cookiesJson, TRUE);
  }
  foreach ($restricted_views as $restricted_view) {
    // Configuration should be set with specific separator.
    /* @see \Drupal\cookies_addons_views\Form\SettingsForm::buildForm() */
    $parts = explode('|', $restricted_view);
    // If there are less or more than 3 parameters, than we need to skip checks.
    if (count($parts) !== 3) {
      continue;
    }

    // Returns FALSE if provided cookies service is already accepted.
    if (!empty($cookies[$parts[2]])) {
      return FALSE;
    }

    // Returns the configured service for the specified view and views display
    // if applicable.
    if ($view_id === $parts[0] && $view_display_id === $parts[1]) {
      return $parts[2];
    }
  }

  // Otherwise return FALSE.
  return FALSE;
}

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

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