Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommentHooks
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 commentLinksAlter
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm\Hook;
6
7use Drupal\comment\CommentInterface;
8use Drupal\Core\Routing\RouteMatchInterface;
9use Drupal\Core\Hook\Attribute\Hook;
10use Drupal\Core\Path\CurrentPathStack;
11use Drupal\Core\Url;
12
13/**
14 * Hooks relating to contacts.
15 */
16class CommentHooks {
17
18  public function __construct(
19    protected RouteMatchInterface $currentRouteMatch,
20    protected CurrentPathStack $pathCurrent,
21  ) {}
22
23  /**
24   * Implements hook_comment_links_alter().
25   */
26  #[Hook('comment_links_alter')]
27  public function commentLinksAlter(array &$links, CommentInterface $comment, array &$context) {
28    $entity_type = $comment->getCommentedEntity()->getEntityTypeId();
29    if ($entity_type !== 'crm_contact') {
30      return;
31    }
32    $current_route = $this->currentRouteMatch->getRouteName();
33    if ($current_route !== 'crm.contact.comment') {
34      return;
35    }
36
37    $current_path = $this->pathCurrent->getPath();
38    $destination = ['destination' => $current_path];
39
40    foreach ($links['comment']['#links'] as &$link) {
41      if (isset($link['url']) && $link['url'] instanceof Url) {
42        $existing_options = $link['url']->getOptions();
43        $link['url']->setOptions([
44          'query' => ($existing_options['query'] ?? []) + $destination,
45        ] + $existing_options);
46      }
47    }
48  }
49
50}