Coverage index

src/Hook/ThemeHooks.php

Lines: 29.63%

LineHitsSource
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Drupal\visitors\Hook;
6
7
use Drupal\Core\Hook\Attribute\Hook;
8
use Drupal\Core\Url;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
/**
12
 * Implements theme preprocess hooks.
13
 */
14
class ThemeHooks {
15
16
  /**
17
   * Constructs a ThemeHooks object.
18
   *
19
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
20
   *   The request stack.
21
   */
22
  public function __construct(
23
    protected RequestStack $requestStack,
243
  ) {}
25
26
  /**
27
   * Implements hook_theme().
28
   *
29
   * @return array
30
   *   Theme hook definitions.
31
   */
32
  #[Hook('theme')]
33
  public function theme(): array {
343
    return [
353
      'visitors_visit_long' => [
363
        'variables' => [
373
          'visit' => NULL,
383
          'actions' => [],
393
        ],
403
      ],
413
    ];
42
  }
43
44
  /**
45
   * Implements hook_preprocess_html().
46
   */
47
  #[Hook('preprocess_html')]
48
  public function preprocessHtml(array &$variables) {
493
    $title = '';
503
    if (isset($variables['head_title']['title'])) {
511
      $title = strip_tags((string) $variables['head_title']['title']);
52
    }
532
    elseif (isset($variables['head_title']) && is_string($variables['head_title'])) {
541
      $title = strip_tags($variables['head_title']);
55
    }
563
    if ($title) {
572
      $variables['#attached']['drupalSettings']['visitors']['title'] = $title;
58
    }
59
  }
60
61
  /**
62
   * Implements hook_preprocess_views_view().
63
   */
64
  #[Hook('preprocess_views_view')]
65
  public function preprocessViewsView(array &$variables) {
66
    // Check if this is a Visitors view that needs sort link cleaning.
670
    $view = $variables['view'];
680
    if ($view->storage->id() !== 'visitors') {
690
      return;
70
    }
71
    // Get excluded parameters from static variable set in ReportController.
720
    $excluded_params =& drupal_static('visitors_excluded_sort_params', []);
730
    $referrer_path =& drupal_static('visitors_referrer_path', '');
740
    if (empty($excluded_params)) {
750
      return;
76
    }
770
    $request = $this->requestStack->getCurrentRequest();
780
    $full_uri = $request ? $request->headers->get('referer') : NULL;
790
    if (isset($variables['exposed']['#action'])) {
800
      $variables['exposed']['#action'] = $full_uri;
81
    }
82
    // Clean sort links by removing AJAX parameters.
830
    if (!empty($variables['header'])) {
840
      $current_query = $request ? $request->query->all() : [];
85
      // Remove excluded parameters.
860
      $clean_query = [];
870
      foreach ($current_query as $key => $value) {
880
        if (!in_array($key, $excluded_params)) {
890
          $clean_query[$key] = $value;
90
        }
91
      }
92
      // Rebuild sort links for each header column.
930
      foreach ($variables['header'] as $field => &$header) {
940
        if (!empty($header['url'])) {
95
          // Parse the existing URL to get the sort parameters.
960
          $url_parts = parse_url($header['url']);
970
          $url_query = [];
980
          if (!empty($url_parts['query'])) {
990
            parse_str($url_parts['query'], $url_query);
100
          }
101
          // Keep only the sort-related parameters and clean query.
1020
          $sort_params = [];
1030
          if (isset($url_query['order'])) {
1040
            $sort_params['order'] = $url_query['order'];
105
          }
1060
          if (isset($url_query['sort'])) {
1070
            $sort_params['sort'] = $url_query['sort'];
108
          }
109
          // Merge clean query with sort parameters.
1100
          $final_query = array_merge($clean_query, $sort_params);
111
          // Rebuild the URL.
1120
          $route_name = $view->live_preview ? '<current>' : '<none>';
1130
          if (!empty($referrer_path)) {
114
            // Use the referrer path for the URL.
1150
            $header['url'] = $referrer_path;
1160
            if (!empty($final_query)) {
1170
              $header['url'] .= '?' . http_build_query($final_query);
118
            }
119
          }
120
          else {
121
            // Fallback to Drupal URL generation.
1220
            $url = new Url($route_name, [], [
1230
              'query' => $final_query,
1240
            ]);
1250
            $header['url'] = $url->toString();
126
          }
127
        }
128
      }
129
    }
130
  }
131
132
}