Lines: 94.17%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Service; | |
| 6 | ||
| 7 | use Drupal\Component\Render\MarkupInterface; | |
| 8 | use Drupal\Core\Access\AccessResult; | |
| 9 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 10 | use Drupal\Core\Controller\TitleResolverInterface; | |
| 11 | use Drupal\Core\Extension\ModuleHandlerInterface; | |
| 12 | use Drupal\Core\Path\CurrentPathStack; | |
| 13 | use Drupal\Core\Routing\RouteMatchInterface; | |
| 14 | use Drupal\Core\Session\AccountInterface; | |
| 15 | use Drupal\Core\Url; | |
| 16 | use Drupal\Core\Utility\Error; | |
| 17 | use Drupal\visitors\VisitorsEventInterface; | |
| 18 | use Drupal\visitors\VisitorsEventPluginManager; | |
| 19 | use Drupal\visitors\VisitorsPageAttachmentsInterface; | |
| 20 | use Drupal\visitors\VisitorsSiteSearchDetectorInterface; | |
| 21 | use Drupal\visitors\VisitorsVisibilityInterface; | |
| 22 | use Psr\Log\LoggerInterface; | |
| 23 | use Symfony\Component\HttpFoundation\RequestStack; | |
| 24 | ||
| 25 | /** | |
| 26 | * Visitors Page Attachments Service. | |
| 27 | */ | |
| 28 | class PageAttachmentsService implements VisitorsPageAttachmentsInterface { | |
| 29 | ||
| 30 | /** | |
| 31 | * The config factory. | |
| 32 | * | |
| 33 | * @var \Drupal\Core\Config\ConfigFactoryInterface | |
| 34 | */ | |
| 35 | protected $configFactory; | |
| 36 | ||
| 37 | /** | |
| 38 | * The current user service. | |
| 39 | * | |
| 40 | * @var \Drupal\Core\Session\AccountInterface | |
| 41 | */ | |
| 42 | protected $currentUser; | |
| 43 | ||
| 44 | /** | |
| 45 | * The module handler service. | |
| 46 | * | |
| 47 | * @var \Drupal\Core\Extension\ModuleHandlerInterface | |
| 48 | */ | |
| 49 | protected $moduleHandler; | |
| 50 | ||
| 51 | /** | |
| 52 | * The route match service. | |
| 53 | * | |
| 54 | * @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface | |
| 55 | */ | |
| 56 | protected $routeMatch; | |
| 57 | ||
| 58 | /** | |
| 59 | * The request stack service. | |
| 60 | * | |
| 61 | * @var \Symfony\Component\HttpFoundation\RequestStack | |
| 62 | */ | |
| 63 | protected $requestStack; | |
| 64 | ||
| 65 | /** | |
| 66 | * The visitors visibility service. | |
| 67 | * | |
| 68 | * @var \Drupal\visitors\VisitorsVisibilityInterface | |
| 69 | */ | |
| 70 | protected $visibilityService; | |
| 71 | ||
| 72 | /** | |
| 73 | * The logger service. | |
| 74 | * | |
| 75 | * @var \Psr\Log\LoggerInterface | |
| 76 | */ | |
| 77 | protected $logger; | |
| 78 | ||
| 79 | /** | |
| 80 | * The visitors event plugin manager. | |
| 81 | * | |
| 82 | * @var \Drupal\visitors\VisitorsEventPluginManager | |
| 83 | */ | |
| 84 | protected $eventPluginManager; | |
| 85 | ||
| 86 | /** | |
| 87 | * The title resolver. | |
| 88 | * | |
| 89 | * @var \Drupal\Core\Controller\TitleResolverInterface | |
| 90 | */ | |
| 91 | protected $titleResolver; | |
| 92 | ||
| 93 | /** | |
| 94 | * The current path. | |
| 95 | * | |
| 96 | * @var \Drupal\Core\Path\CurrentPathStack | |
| 97 | */ | |
| 98 | protected $currentPath; | |
| 99 | ||
| 100 | /** | |
| 101 | * Custom events queued for this page. | |
| 102 | * | |
| 103 | * @var \Drupal\visitors\VisitorsEventInterface | |
| 104 | */ | |
| 105 | protected $events; | |
| 106 | ||
| 107 | /** | |
| 108 | * The site search detector. | |
| 109 | * | |
| 110 | * @var \Drupal\visitors\VisitorsSiteSearchDetectorInterface | |
| 111 | */ | |
| 112 | protected $siteSearch; | |
| 113 | ||
| 114 | /** | |
| 115 | * Constructs a new Page Attachments Service. | |
| 116 | * | |
| 117 | * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | |
| 118 | * The config factory. | |
| 119 | * @param \Drupal\Core\Session\AccountInterface $current_user | |
| 120 | * The current user service. | |
| 121 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler | |
| 122 | * The module handler service. | |
| 123 | * @param \Drupal\Core\Routing\ResettableStackedRouteMatchInterface $route_match | |
| 124 | * The route match service. | |
| 125 | * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack | |
| 126 | * The request stack service. | |
| 127 | * @param \Drupal\visitors\VisitorsVisibilityInterface $visibility_service | |
| 128 | * The visitors visibility service. | |
| 129 | * @param \Psr\Log\LoggerInterface $logger | |
| 130 | * The logger service. | |
| 131 | * @param \Drupal\visitors\VisitorsEventPluginManager $event_plugin_manager | |
| 132 | * The visitors event plugin manager. | |
| 133 | * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver | |
| 134 | * The title resolver. | |
| 135 | * @param \Drupal\Core\Path\CurrentPathStack $current_path | |
| 136 | * The current path. | |
| 137 | * @param \Drupal\visitors\VisitorsEventInterface $events | |
| 138 | * Custom events queued for this page. | |
| 139 | * @param \Drupal\visitors\VisitorsSiteSearchDetectorInterface $siteSearch | |
| 140 | * The site search detector. | |
| 141 | */ | |
| 142 | public function __construct( | |
| 143 | ConfigFactoryInterface $config_factory, | |
| 144 | AccountInterface $current_user, | |
| 145 | ModuleHandlerInterface $module_handler, | |
| 146 | RouteMatchInterface $route_match, | |
| 147 | RequestStack $request_stack, | |
| 148 | VisitorsVisibilityInterface $visibility_service, | |
| 149 | LoggerInterface $logger, | |
| 150 | VisitorsEventPluginManager $event_plugin_manager, | |
| 151 | TitleResolverInterface $title_resolver, | |
| 152 | CurrentPathStack $current_path, | |
| 153 | VisitorsEventInterface $events, | |
| 154 | VisitorsSiteSearchDetectorInterface $siteSearch, | |
| 155 | ) { | |
| 156 | 16 | $this->configFactory = $config_factory; |
| 157 | 16 | $this->currentUser = $current_user; |
| 158 | 16 | $this->moduleHandler = $module_handler; |
| 159 | 16 | $this->routeMatch = $route_match; |
| 160 | 16 | $this->requestStack = $request_stack; |
| 161 | 16 | $this->visibilityService = $visibility_service; |
| 162 | 16 | $this->logger = $logger; |
| 163 | 16 | $this->eventPluginManager = $event_plugin_manager; |
| 164 | 16 | $this->titleResolver = $title_resolver; |
| 165 | 16 | $this->currentPath = $current_path; |
| 166 | 16 | $this->events = $events; |
| 167 | 16 | $this->siteSearch = $siteSearch; |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * {@inheritdoc} | |
| 172 | */ | |
| 173 | public function pageAttachments(array &$page) { | |
| 174 | 2 | $page['#cache']['tags'][] = 'user:' . $this->currentUser->id(); |
| 175 | 2 | $page['#cache']['contexts'][] = 'user'; |
| 176 | 2 | $page['#cache']['tags'][] = 'config:visitors.settings'; |
| 177 | ||
| 178 | 2 | $this->attachToolbar($page); |
| 179 | ||
| 180 | try { | |
| 181 | 2 | if ($this->visibilityService->isVisible()) { |
| 182 | 1 | $this->attachMetaData($page); |
| 183 | 1 | $this->attachEntityCounter($page); |
| 184 | 1 | $this->processEvent($page); |
| 185 | } | |
| 186 | } | |
| 187 | 1 | catch (\Exception $e) { |
| 188 | 1 | Error::logException($this->logger, $e); |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | /** | |
| 193 | * {@inheritdoc} | |
| 194 | */ | |
| 195 | public function pageBottom(array &$page_bottom): void { | |
| 196 | 4 | $page_bottom['visitors_noscript'] = [ |
| 197 | 4 | '#cache' => [ |
| 198 | 4 | 'contexts' => ['user', 'url'], |
| 199 | 4 | 'tags' => ['config:visitors.settings'], |
| 200 | 4 | ], |
| 201 | 4 | ]; |
| 202 | ||
| 203 | try { | |
| 204 | 4 | if (!$this->visibilityService->isVisible()) { |
| 205 | 1 | return; |
| 206 | } | |
| 207 | 3 | $page_bottom['visitors_noscript'] += $this->buildNoScript(); |
| 208 | 3 | $this->attachQueuedEvents($page_bottom); |
| 209 | } | |
| 210 | 0 | catch (\Exception $e) { |
| 211 | 0 | Error::logException($this->logger, $e); |
| 212 | } | |
| 213 | } | |
| 214 | ||
| 215 | /** | |
| 216 | * Copies queued trackEvent calls into drupalSettings. | |
| 217 | * | |
| 218 | * @param array $page_bottom | |
| 219 | * The page bottom render array. | |
| 220 | */ | |
| 221 | protected function attachQueuedEvents(array &$page_bottom): void { | |
| 222 | 3 | $events = $this->events->getQueuedEvents(); |
| 223 | 3 | if ($events === []) { |
| 224 | 2 | return; |
| 225 | } | |
| 226 | 1 | $page_bottom['#attached']['drupalSettings']['visitors']['events'] = $events; |
| 227 | 1 | $page_bottom['#attached']['library'][] = 'visitors/visitors'; |
| 228 | } | |
| 229 | ||
| 230 | /** | |
| 231 | * Builds the noscript image beacon. | |
| 232 | * | |
| 233 | * Browsers that run JavaScript do not request this image. | |
| 234 | * | |
| 235 | * @return array | |
| 236 | * The noscript render element, without cache metadata. | |
| 237 | */ | |
| 238 | protected function buildNoScript(): array { | |
| 239 | 3 | $request = $this->requestStack->getCurrentRequest(); |
| 240 | 3 | $settings = $this->configFactory->get('visitors.settings'); |
| 241 | 3 | $query = [ |
| 242 | 3 | 'send_image' => '1', |
| 243 | 3 | 'rec' => '1', |
| 244 | 3 | 'url' => $request ? $request->getUri() : '', |
| 245 | 3 | 'action_name' => $this->getActionName(), |
| 246 | 3 | 'cvar' => $this->getCustomVariables(), |
| 247 | 3 | ]; |
| 248 | 3 | if ($settings->get('track.userid') !== FALSE) { |
| 249 | 1 | $query['uid'] = (string) $this->currentUser->id(); |
| 250 | } | |
| 251 | 3 | $event_variables = $this->getEventVariables(); |
| 252 | 3 | if ($event_variables !== NULL) { |
| 253 | 1 | $query['e_cvar'] = $event_variables; |
| 254 | } | |
| 255 | ||
| 256 | 3 | $url = Url::fromRoute('visitors.track', [], [ |
| 257 | 3 | 'query' => $query, |
| 258 | 3 | ])->toString(); |
| 259 | ||
| 260 | 3 | return [ |
| 261 | 3 | '#type' => 'html_tag', |
| 262 | 3 | '#tag' => 'noscript', |
| 263 | 3 | 'paragraph' => [ |
| 264 | 3 | '#type' => 'html_tag', |
| 265 | 3 | '#tag' => 'p', |
| 266 | 3 | 'image' => [ |
| 267 | 3 | '#type' => 'html_tag', |
| 268 | 3 | '#tag' => 'img', |
| 269 | 3 | '#attributes' => [ |
| 270 | 3 | 'src' => $url, |
| 271 | 3 | 'style' => 'border:0', |
| 272 | 3 | 'alt' => '', |
| 273 | 3 | 'width' => '1', |
| 274 | 3 | 'height' => '1', |
| 275 | 3 | ], |
| 276 | 3 | ], |
| 277 | 3 | ], |
| 278 | 3 | ]; |
| 279 | } | |
| 280 | ||
| 281 | /** | |
| 282 | * Page title sent as the tracking action name. | |
| 283 | * | |
| 284 | * @return string | |
| 285 | * The plain-text page title, or an empty string. | |
| 286 | */ | |
| 287 | protected function getActionName(): string { | |
| 288 | 3 | $route = $this->routeMatch->getRouteObject(); |
| 289 | 3 | $request = $this->requestStack->getCurrentRequest(); |
| 290 | 3 | if (!$route || !$request) { |
| 291 | 1 | return ''; |
| 292 | } | |
| 293 | ||
| 294 | 2 | $title = $this->titleResolver->getTitle($request, $route); |
| 295 | 2 | return $this->plainTitle($title); |
| 296 | } | |
| 297 | ||
| 298 | /** | |
| 299 | * Converts a route title to plain text. | |
| 300 | * | |
| 301 | * @param mixed $title | |
| 302 | * The title from the title resolver. | |
| 303 | */ | |
| 304 | protected function plainTitle(mixed $title): string { | |
| 305 | 2 | if ($title === NULL) { |
| 306 | 0 | return ''; |
| 307 | } | |
| 308 | 2 | if (is_array($title)) { |
| 309 | 0 | if (isset($title['#plain_text'])) { |
| 310 | 0 | $title = $title['#plain_text']; |
| 311 | } | |
| 312 | 0 | elseif (isset($title['#markup'])) { |
| 313 | 0 | $title = $title['#markup']; |
| 314 | } | |
| 315 | else { | |
| 316 | 0 | return ''; |
| 317 | } | |
| 318 | } | |
| 319 | 2 | $is_stringable = is_scalar($title) |
| 320 | 2 | || $title instanceof MarkupInterface |
| 321 | 2 | || $title instanceof \Stringable; |
| 322 | 2 | if (!$is_stringable) { |
| 323 | 0 | return ''; |
| 324 | } | |
| 325 | ||
| 326 | 2 | return trim(strip_tags((string) $title)); |
| 327 | } | |
| 328 | ||
| 329 | /** | |
| 330 | * JSON page-scope custom variables for the image beacon. | |
| 331 | * | |
| 332 | * @return string | |
| 333 | * The encoded custom variables. | |
| 334 | */ | |
| 335 | protected function getCustomVariables(): string { | |
| 336 | 3 | $path = $this->currentPath->getPath(); |
| 337 | 3 | if ($path === '' || $path[0] !== '/') { |
| 338 | 0 | $path = '/' . $path; |
| 339 | } | |
| 340 | 3 | $server = gethostname(); |
| 341 | ||
| 342 | 3 | $cvar = [ |
| 343 | 3 | '1' => ['route', (string) $this->routeMatch->getRouteName()], |
| 344 | 3 | '2' => ['path', $path], |
| 345 | 3 | '3' => ['server', $server === FALSE ? '' : $server], |
| 346 | 3 | ]; |
| 347 | 3 | $counter = $this->getEntityCounter(); |
| 348 | 3 | if ($counter !== NULL) { |
| 349 | 1 | $cvar['4'] = ['viewed', $counter]; |
| 350 | } | |
| 351 | ||
| 352 | 3 | return (string) json_encode( |
| 353 | 3 | $cvar, |
| 354 | 3 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
| 355 | 3 | ); |
| 356 | } | |
| 357 | ||
| 358 | /** | |
| 359 | * JSON event-scope custom variables for the image beacon. | |
| 360 | * | |
| 361 | * Indexes match js/visitors.js: plugin is 5, event is 6, and | |
| 362 | * plugin variables start at 7. | |
| 363 | * | |
| 364 | * @return string|null | |
| 365 | * The encoded event variables, or NULL when there is no event. | |
| 366 | */ | |
| 367 | protected function getEventVariables(): ?string { | |
| 368 | 3 | $event = $this->getEvent(); |
| 369 | 3 | if ($event === NULL) { |
| 370 | 2 | return NULL; |
| 371 | } | |
| 372 | ||
| 373 | 1 | $e_cvar = []; |
| 374 | 1 | if (!empty($event['plugin'])) { |
| 375 | 1 | $e_cvar['5'] = ['plugin', $event['plugin']]; |
| 376 | } | |
| 377 | 1 | if (!empty($event['event'])) { |
| 378 | 1 | $e_cvar['6'] = ['event', $event['event']]; |
| 379 | } | |
| 380 | 1 | $variables = $event['variables'] ?? NULL; |
| 381 | 1 | if (is_array($variables) && $variables !== []) { |
| 382 | 1 | $index = 7; |
| 383 | 1 | foreach ($variables as $key => $value) { |
| 384 | 1 | $e_cvar[(string) $index] = [(string) $key, $value]; |
| 385 | 1 | $index++; |
| 386 | } | |
| 387 | } | |
| 388 | 1 | if ($e_cvar === []) { |
| 389 | 0 | return NULL; |
| 390 | } | |
| 391 | ||
| 392 | 1 | return (string) json_encode( |
| 393 | 1 | $e_cvar, |
| 394 | 1 | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
| 395 | 1 | ); |
| 396 | } | |
| 397 | ||
| 398 | /** | |
| 399 | * Attach meta data to the page. | |
| 400 | * | |
| 401 | * @param array $page | |
| 402 | * The page attachments array. | |
| 403 | */ | |
| 404 | protected function attachMetaData(array &$page): void { | |
| 405 | 3 | $route = $this->routeMatch->getRouteName(); |
| 406 | 3 | $request = $this->requestStack->getCurrentRequest(); |
| 407 | 3 | $base_path = $request->getBasePath(); |
| 408 | 3 | $module_path = $this->moduleHandler->getModule('visitors')->getPath(); |
| 409 | ||
| 410 | 3 | $settings = $this->configFactory->get('visitors.settings'); |
| 411 | ||
| 412 | 3 | $visitors = &$page['#attached']['drupalSettings']['visitors']; |
| 413 | ||
| 414 | 3 | $visitors['module'] = "$base_path/$module_path"; |
| 415 | 3 | $visitors['route'] = $route; |
| 416 | 3 | $visitors['server'] = gethostname(); |
| 417 | 3 | $visitors['debug'] = (bool) $settings->get('client_debug'); |
| 418 | 3 | if ($settings->get('scroll_depth')) { |
| 419 | 1 | $visitors['scroll_depth'] = TRUE; |
| 420 | } | |
| 421 | 3 | $javascript_errors = $settings->get('javascript_errors'); |
| 422 | 3 | if (in_array($javascript_errors, ['activity', 'drupal'], TRUE)) { |
| 423 | 2 | $visitors['javascript_errors'] = TRUE; |
| 424 | } | |
| 425 | 3 | $visitors['track_downloads'] = (bool) ($settings->get('track_downloads') ?? TRUE); |
| 426 | 3 | $visitors['track_outlinks'] = (bool) ($settings->get('track_outlinks') ?? TRUE); |
| 427 | 3 | $visitors['track_media'] = (bool) ($settings->get('track_media') ?? TRUE); |
| 428 | 3 | $heartbeat = $this->heartbeatInterval($settings->get('heartbeat_interval')); |
| 429 | 3 | if ($heartbeat >= 5) { |
| 430 | 2 | $visitors['heartbeat_interval'] = $heartbeat; |
| 431 | } | |
| 432 | 3 | $visitors['track_userid'] = $settings->get('track.userid') !== FALSE; |
| 433 | 3 | $visitors['cookies'] = (bool) ($settings->get('cookies') ?? FALSE); |
| 434 | 3 | $search_count = $this->siteSearch->resultCount(); |
| 435 | 3 | if ($search_count !== NULL) { |
| 436 | 0 | $visitors['search_count'] = $search_count; |
| 437 | } | |
| 438 | 3 | $page['#attached']['library'][] = 'visitors/visitors'; |
| 439 | ||
| 440 | } | |
| 441 | ||
| 442 | /** | |
| 443 | * Normalizes the heartbeat interval sent to the browser. | |
| 444 | * | |
| 445 | * A missing value is 15 seconds. Zero turns the heartbeat off. | |
| 446 | * | |
| 447 | * @param mixed $value | |
| 448 | * The stored interval. | |
| 449 | * | |
| 450 | * @return int | |
| 451 | * The interval in seconds. | |
| 452 | */ | |
| 453 | protected function heartbeatInterval(mixed $value): int { | |
| 454 | 3 | if ($value === NULL || $value === '') { |
| 455 | 2 | return 15; |
| 456 | } | |
| 457 | 1 | $interval = (int) $value; |
| 458 | 1 | if ($interval <= 0) { |
| 459 | 1 | return 0; |
| 460 | } | |
| 461 | 0 | return max(5, $interval); |
| 462 | } | |
| 463 | ||
| 464 | /** | |
| 465 | * Visitors toolbar and navigation integration. | |
| 466 | * | |
| 467 | * @param array $page | |
| 468 | * The page attachments array. | |
| 469 | */ | |
| 470 | protected function attachToolbar(array &$page): void { | |
| 471 | 6 | $toolbar_access = AccessResult::allowedIfHasPermissions($this->currentUser, [ |
| 472 | 6 | 'access visitors', |
| 473 | 6 | 'access toolbar', |
| 474 | 6 | ]); |
| 475 | 6 | if ($toolbar_access->isAllowed()) { |
| 476 | 1 | $page['#attached']['library'][] = 'visitors/toolbar'; |
| 477 | } | |
| 478 | ||
| 479 | 6 | if (!$this->moduleHandler->moduleExists('navigation')) { |
| 480 | 5 | return; |
| 481 | } | |
| 482 | ||
| 483 | 1 | $navigation_access = AccessResult::allowedIfHasPermissions($this->currentUser, [ |
| 484 | 1 | 'access visitors', |
| 485 | 1 | 'access navigation', |
| 486 | 1 | ]); |
| 487 | 1 | if ($navigation_access->isAllowed()) { |
| 488 | 1 | $page['#attached']['library'][] = 'visitors/navigation'; |
| 489 | } | |
| 490 | } | |
| 491 | ||
| 492 | /** | |
| 493 | * Attach entity counter to the page. | |
| 494 | * | |
| 495 | * @param array $page | |
| 496 | * The page attachments array. | |
| 497 | */ | |
| 498 | protected function attachEntityCounter(array &$page): void { | |
| 499 | 3 | $counter = $this->getEntityCounter(); |
| 500 | 3 | if ($counter === NULL) { |
| 501 | 2 | return; |
| 502 | } | |
| 503 | 1 | $page['#attached']['drupalSettings']['visitors']['counter'] = $counter; |
| 504 | } | |
| 505 | ||
| 506 | /** | |
| 507 | * Entity counter value for the current canonical route. | |
| 508 | * | |
| 509 | * @return string|null | |
| 510 | * The entity type and id, or NULL when the counter does not apply. | |
| 511 | */ | |
| 512 | protected function getEntityCounter(): ?string { | |
| 513 | 6 | $route = $this->routeMatch->getRouteName() ?? ''; |
| 514 | 6 | $route_array = explode('.', $route); |
| 515 | 6 | $is_canonical = count($route_array) == 3 |
| 516 | 6 | && $route_array[0] == 'entity' |
| 517 | 6 | && $route_array[2] == 'canonical'; |
| 518 | 6 | if (!$is_canonical) { |
| 519 | 1 | return NULL; |
| 520 | } | |
| 521 | ||
| 522 | 5 | $entity_type = $route_array[1]; |
| 523 | 5 | $settings = $this->configFactory->get('visitors.settings'); |
| 524 | 5 | $entity_types = $settings->get('counter.entity_types') ?? []; |
| 525 | 5 | if (!$settings->get('counter.enabled') || !in_array($entity_type, $entity_types)) { |
| 526 | 3 | return NULL; |
| 527 | } | |
| 528 | ||
| 529 | 2 | $entity_id = $this->routeMatch->getParameter($entity_type)->id(); |
| 530 | 2 | return "$entity_type:$entity_id"; |
| 531 | } | |
| 532 | ||
| 533 | /** | |
| 534 | * Process visitor events. | |
| 535 | * | |
| 536 | * @param array $page | |
| 537 | * The page attachments array. | |
| 538 | */ | |
| 539 | protected function processEvent(array &$page): void { | |
| 540 | 2 | $event = $this->getEvent(); |
| 541 | 2 | if ($event === NULL) { |
| 542 | 1 | return; |
| 543 | } | |
| 544 | 1 | $page['#attached']['drupalSettings']['visitors']['event'] = $event; |
| 545 | } | |
| 546 | ||
| 547 | /** | |
| 548 | * The first matching visitor event for this request. | |
| 549 | * | |
| 550 | * @return array|null | |
| 551 | * Plugin id, event name, and variables, or NULL. | |
| 552 | */ | |
| 553 | protected function getEvent(): ?array { | |
| 554 | 5 | $route = $this->routeMatch->getRouteName(); |
| 555 | 5 | $request = $this->requestStack->getCurrentRequest(); |
| 556 | ||
| 557 | 5 | $path = $request->getPathInfo(); |
| 558 | 5 | $context = [ |
| 559 | 5 | 'url' => $request->getUri(), |
| 560 | 5 | 'path' => $path, |
| 561 | 5 | 'route' => $route, |
| 562 | 5 | 'uid' => $this->currentUser->id(), |
| 563 | 5 | ]; |
| 564 | ||
| 565 | // Get all plugins sorted by weight. | |
| 566 | 5 | $definitions = $this->eventPluginManager->getDefinitions(); |
| 567 | ||
| 568 | 5 | uasort($definitions, function ($a, $b) { |
| 569 | 1 | return ($a['weight'] ?? 0) <=> ($b['weight'] ?? 0); |
| 570 | 5 | }); |
| 571 | ||
| 572 | // Process each plugin until one returns an array. | |
| 573 | 5 | foreach ($definitions as $plugin_id => $definition) { |
| 574 | 2 | $plugin = $this->eventPluginManager->createInstance($plugin_id); |
| 575 | 2 | $result = $plugin->process($context); |
| 576 | ||
| 577 | 2 | if ($result !== NULL) { |
| 578 | 2 | return [ |
| 579 | 2 | 'plugin' => $plugin->getPluginId(), |
| 580 | 2 | 'event' => $result['event'], |
| 581 | 2 | 'variables' => $result['variables'], |
| 582 | 2 | ]; |
| 583 | } | |
| 584 | } | |
| 585 | ||
| 586 | 3 | return NULL; |
| 587 | } | |
| 588 | ||
| 589 | } |