Coverage index

visitors.module

Lines: 83.82%

LineHitsSource
1
<?php
2
3
/**
4
 * @file
5
 * Logs visitors for your site.
6
 */
7
8
use Drupal\Core\Entity\EntityInterface;
9
use Drupal\Core\Form\FormStateInterface;
10
use Drupal\Core\Hook\Attribute\LegacyHook;
11
use Drupal\Core\Render\BubbleableMetadata;
12
use Drupal\node\NodeInterface;
13
use Drupal\visitors\Hook\CounterHooks;
14
use Drupal\visitors\Hook\CronHooks;
15
use Drupal\visitors\Hook\FormHooks;
16
use Drupal\visitors\Hook\HelpHooks;
17
use Drupal\visitors\Hook\PageAttachmentsHooks;
18
use Drupal\visitors\Hook\SearchHooks;
19
use Drupal\visitors\Hook\ThemeHooks;
20
use Drupal\visitors\Hook\TokenHooks;
21
use Drupal\visitors\Hook\UserHooks;
22
use Drupal\visitors\Hook\ViewsHooks;
23
24
/**
25
 * Returns a hook class, instantiating directly when unavailable.
26
 *
27
 * Unit tests typically register only the services they exercise directly and
28
 * omit the hook-class service. This helper builds the hook from those
29
 * services when the hook service itself is not registered.
30
 *
31
 * @param class-string<T> $class
32
 *   The hook class to load.
33
 *
34
 * @return T
35
 *   The hook class instance.
36
 *
37
 * @template T of object
38
 */
39
function _visitors_hook(string $class): object {
4025
  if (\Drupal::hasService($class)) {
413
    return \Drupal::service($class);
42
  }
43
44
  // Unit tests register the services a hook uses and omit the hook service.
4522
  $container = \Drupal::getContainer();
4622
  return match ($class) {
475
    CounterHooks::class => new CounterHooks(
485
      $container->get('current_user'),
495
      $container->get('config.factory'),
505
      $container->get('visitors.counter'),
515
      $container->get('string_translation'),
525
    ),
531
    CronHooks::class => new CronHooks(
541
      $container->get('visitors.cron'),
551
    ),
562
    PageAttachmentsHooks::class => new PageAttachmentsHooks(
572
      $container->get('visitors.page_attachments'),
582
    ),
590
    SearchHooks::class => new SearchHooks(
600
      $container->get('config.factory'),
610
      $container->get('state'),
620
      $container->get('string_translation'),
630
      $container->get('request_stack'),
640
      $container->get('pager.manager'),
650
      $container->get('visitors.site_search'),
660
    ),
673
    ThemeHooks::class => new ThemeHooks(
683
      $container->get('request_stack'),
693
    ),
703
    TokenHooks::class => new TokenHooks(
713
      $container->get('config.factory'),
723
      $container->get('token'),
733
      $container->get('visitors.counter'),
743
      $container->get('date.formatter'),
753
      $container->get('string_translation'),
763
    ),
776
    UserHooks::class => new UserHooks(
786
      $container->get('config.factory'),
796
      $container->get('user.data'),
806
      $container->get('string_translation'),
816
    ),
821
    ViewsHooks::class => new ViewsHooks(
831
      $container->get('config.factory'),
841
      $container->get('entity_type.manager'),
851
      $container->get('string_translation'),
861
    ),
8722
    default => new $class(),
8822
  };
89
}
90
91
/**
92
 * Implements hook_help().
93
 */
94
#[LegacyHook]
95
function visitors_help($route_name, $route_match) {
961
  return _visitors_hook(HelpHooks::class)->help($route_name, $route_match);
97
}
98
99
/**
100
 * Implements hook_cron().
101
 */
102
#[LegacyHook]
103
function visitors_cron(): void {
1041
  _visitors_hook(CronHooks::class)->cron();
105
}
106
107
/**
108
 * Implements hook_page_attachments().
109
 */
110
#[LegacyHook]
111
function visitors_page_attachments(array &$page) {
1121
  _visitors_hook(PageAttachmentsHooks::class)->pageAttachments($page);
113
}
114
115
/**
116
 * Implements hook_page_bottom().
117
 */
118
#[LegacyHook]
119
function visitors_page_bottom(array &$page_bottom): void {
1201
  _visitors_hook(PageAttachmentsHooks::class)->pageBottom($page_bottom);
121
}
122
123
/**
124
 * Implements hook_form_FORM_ID_alter().
125
 *
126
 * Allow users to decide if tracking code will be added to pages or not.
127
 */
128
#[LegacyHook]
129
function visitors_form_user_form_alter(&$form, FormStateInterface $form_state) {
1306
  _visitors_hook(UserHooks::class)->formUserFormAlter($form, $form_state);
131
}
132
133
/**
134
 * Submit callback for user profile form to save the Visitor setting.
135
 */
136
function visitors_user_profile_form_submit($form, FormStateInterface $form_state) {
1373
  if (!$form_state->hasValue('user_account_users')) {
1381
    return;
139
  }
140
  /** @var \Drupal\user\AccountForm $user_form */
1412
  $user_form = $form_state->getFormObject();
142
  /** @var \Drupal\user\UserInterface $account */
1432
  $account = $user_form->getEntity();
144
1452
  $value = (int) $form_state->getValue('user_account_users');
1462
  \Drupal::service('user.data')
1472
    ->set('visitors', $account->id(), 'user_account_users', $value);
148
}
149
150
/**
151
 * Implements hook_node_links_alter().
152
 */
153
#[LegacyHook]
154
function visitors_node_links_alter(array &$links, NodeInterface $entity, array &$context) {
1553
  return _visitors_hook(CounterHooks::class)->nodeLinksAlter($links, $entity, $context);
156
}
157
158
/**
159
 * Implements hook_entity_delete().
160
 */
161
#[LegacyHook]
162
function visitors_entity_delete(EntityInterface $entity) {
1632
  _visitors_hook(CounterHooks::class)->entityDelete($entity);
164
}
165
166
/**
167
 * Implements hook_ranking().
168
 */
169
#[LegacyHook]
170
function visitors_ranking() {
1710
  return _visitors_hook(SearchHooks::class)->ranking();
172
}
173
174
/**
175
 * Implements hook_node_search_ranking().
176
 */
177
#[LegacyHook]
178
function visitors_node_search_ranking() {
1790
  return _visitors_hook(SearchHooks::class)->ranking();
180
}
181
182
/**
183
 * Implements hook_views_data().
184
 */
185
#[LegacyHook]
186
function visitors_views_data() {
1871
  return _visitors_hook(ViewsHooks::class)->viewsData();
188
}
189
190
/**
191
 * Implements hook_token_info().
192
 */
193
#[LegacyHook]
194
function visitors_token_info() {
1951
  return _visitors_hook(TokenHooks::class)->tokenInfo();
196
}
197
198
/**
199
 * Implements hook_tokens().
200
 */
201
#[LegacyHook]
202
function visitors_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
2032
  return _visitors_hook(TokenHooks::class)->tokens($type, $tokens, $data, $options, $bubbleable_metadata);
204
}
205
206
/**
207
 * Implements hook_form_alter().
208
 */
209
#[LegacyHook]
210
function visitors_form_alter(&$form, FormStateInterface $form_state, $form_id) {
2113
  _visitors_hook(FormHooks::class)->formAlter($form, $form_state, $form_id);
212
}
213
214
/**
215
 * Implements hook_preprocess_html().
216
 */
217
#[LegacyHook]
218
function visitors_preprocess_html(array &$variables) {
2193
  _visitors_hook(ThemeHooks::class)->preprocessHtml($variables);
220
}
221
222
/**
223
 * Implements hook_preprocess_views_view().
224
 */
225
#[LegacyHook]
226
function visitors_preprocess_views_view(array &$variables) {
2270
  _visitors_hook(ThemeHooks::class)->preprocessViewsView($variables);
228
}