Lines: 29.63%
| Line | Hits | Source |
|---|---|---|
| 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, | |
| 24 | 3 | ) {} |
| 25 | ||
| 26 | /** | |
| 27 | * Implements hook_theme(). | |
| 28 | * | |
| 29 | * @return array | |
| 30 | * Theme hook definitions. | |
| 31 | */ | |
| 32 | #[Hook('theme')] | |
| 33 | public function theme(): array { | |
| 34 | 3 | return [ |
| 35 | 3 | 'visitors_visit_long' => [ |
| 36 | 3 | 'variables' => [ |
| 37 | 3 | 'visit' => NULL, |
| 38 | 3 | 'actions' => [], |
| 39 | 3 | ], |
| 40 | 3 | ], |
| 41 | 3 | ]; |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Implements hook_preprocess_html(). | |
| 46 | */ | |
| 47 | #[Hook('preprocess_html')] | |
| 48 | public function preprocessHtml(array &$variables) { | |
| 49 | 3 | $title = ''; |
| 50 | 3 | if (isset($variables['head_title']['title'])) { |
| 51 | 1 | $title = strip_tags((string) $variables['head_title']['title']); |
| 52 | } | |
| 53 | 2 | elseif (isset($variables['head_title']) && is_string($variables['head_title'])) { |
| 54 | 1 | $title = strip_tags($variables['head_title']); |
| 55 | } | |
| 56 | 3 | if ($title) { |
| 57 | 2 | $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. | |
| 67 | 0 | $view = $variables['view']; |
| 68 | 0 | if ($view->storage->id() !== 'visitors') { |
| 69 | 0 | return; |
| 70 | } | |
| 71 | // Get excluded parameters from static variable set in ReportController. | |
| 72 | 0 | $excluded_params =& drupal_static('visitors_excluded_sort_params', []); |
| 73 | 0 | $referrer_path =& drupal_static('visitors_referrer_path', ''); |
| 74 | 0 | if (empty($excluded_params)) { |
| 75 | 0 | return; |
| 76 | } | |
| 77 | 0 | $request = $this->requestStack->getCurrentRequest(); |
| 78 | 0 | $full_uri = $request ? $request->headers->get('referer') : NULL; |
| 79 | 0 | if (isset($variables['exposed']['#action'])) { |
| 80 | 0 | $variables['exposed']['#action'] = $full_uri; |
| 81 | } | |
| 82 | // Clean sort links by removing AJAX parameters. | |
| 83 | 0 | if (!empty($variables['header'])) { |
| 84 | 0 | $current_query = $request ? $request->query->all() : []; |
| 85 | // Remove excluded parameters. | |
| 86 | 0 | $clean_query = []; |
| 87 | 0 | foreach ($current_query as $key => $value) { |
| 88 | 0 | if (!in_array($key, $excluded_params)) { |
| 89 | 0 | $clean_query[$key] = $value; |
| 90 | } | |
| 91 | } | |
| 92 | // Rebuild sort links for each header column. | |
| 93 | 0 | foreach ($variables['header'] as $field => &$header) { |
| 94 | 0 | if (!empty($header['url'])) { |
| 95 | // Parse the existing URL to get the sort parameters. | |
| 96 | 0 | $url_parts = parse_url($header['url']); |
| 97 | 0 | $url_query = []; |
| 98 | 0 | if (!empty($url_parts['query'])) { |
| 99 | 0 | parse_str($url_parts['query'], $url_query); |
| 100 | } | |
| 101 | // Keep only the sort-related parameters and clean query. | |
| 102 | 0 | $sort_params = []; |
| 103 | 0 | if (isset($url_query['order'])) { |
| 104 | 0 | $sort_params['order'] = $url_query['order']; |
| 105 | } | |
| 106 | 0 | if (isset($url_query['sort'])) { |
| 107 | 0 | $sort_params['sort'] = $url_query['sort']; |
| 108 | } | |
| 109 | // Merge clean query with sort parameters. | |
| 110 | 0 | $final_query = array_merge($clean_query, $sort_params); |
| 111 | // Rebuild the URL. | |
| 112 | 0 | $route_name = $view->live_preview ? '<current>' : '<none>'; |
| 113 | 0 | if (!empty($referrer_path)) { |
| 114 | // Use the referrer path for the URL. | |
| 115 | 0 | $header['url'] = $referrer_path; |
| 116 | 0 | if (!empty($final_query)) { |
| 117 | 0 | $header['url'] .= '?' . http_build_query($final_query); |
| 118 | } | |
| 119 | } | |
| 120 | else { | |
| 121 | // Fallback to Drupal URL generation. | |
| 122 | 0 | $url = new Url($route_name, [], [ |
| 123 | 0 | 'query' => $final_query, |
| 124 | 0 | ]); |
| 125 | 0 | $header['url'] = $url->toString(); |
| 126 | } | |
| 127 | } | |
| 128 | } | |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | } |