Lines: 100.00%
| Line | Hits | Source |
|---|---|---|
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Drupal\visitors\Hook; | |
| 6 | ||
| 7 | use Drupal\Core\Config\ConfigFactoryInterface; | |
| 8 | use Drupal\Core\Entity\EntityInterface; | |
| 9 | use Drupal\Core\Hook\Attribute\Hook; | |
| 10 | use Drupal\Core\Session\AccountInterface; | |
| 11 | use Drupal\Core\StringTranslation\TranslationInterface; | |
| 12 | use Drupal\node\NodeInterface; | |
| 13 | use Drupal\visitors\VisitorsCounterInterface; | |
| 14 | ||
| 15 | /** | |
| 16 | * Implements counter hooks for nodes and entities. | |
| 17 | */ | |
| 18 | class CounterHooks { | |
| 19 | ||
| 20 | /** | |
| 21 | * Constructs a CounterHooks object. | |
| 22 | * | |
| 23 | * @param \Drupal\Core\Session\AccountInterface $currentUser | |
| 24 | * The current user. | |
| 25 | * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory | |
| 26 | * The config factory. | |
| 27 | * @param \Drupal\visitors\VisitorsCounterInterface $counter | |
| 28 | * The visitors counter. | |
| 29 | * @param \Drupal\Core\StringTranslation\TranslationInterface $translation | |
| 30 | * The string translation service. | |
| 31 | */ | |
| 32 | public function __construct( | |
| 33 | protected AccountInterface $currentUser, | |
| 34 | protected ConfigFactoryInterface $configFactory, | |
| 35 | protected VisitorsCounterInterface $counter, | |
| 36 | protected TranslationInterface $translation, | |
| 37 | 5 | ) {} |
| 38 | ||
| 39 | /** | |
| 40 | * Implements hook_node_links_alter(). | |
| 41 | */ | |
| 42 | #[Hook('node_links_alter')] | |
| 43 | public function nodeLinksAlter(array &$links, NodeInterface $entity, array &$context) { | |
| 44 | 3 | if ($context['view_mode'] == 'rss') { |
| 45 | 1 | return NULL; |
| 46 | } | |
| 47 | 2 | $links['#cache']['contexts'][] = 'user.permissions'; |
| 48 | 2 | if (!$this->currentUser->hasPermission('view visitors counter')) { |
| 49 | 1 | return NULL; |
| 50 | } | |
| 51 | 1 | $settings = $this->configFactory->get('visitors.settings'); |
| 52 | 1 | $entity_id = (int) $entity->id(); |
| 53 | 1 | $statistics = $this->counter->fetchView('node', $entity_id); |
| 54 | 1 | if ($statistics) { |
| 55 | 1 | $statistics_links['visitors_counter']['title'] = $this->translation |
| 56 | 1 | ->formatPlural($statistics->getTotalCount(), '1 view', '@count views'); |
| 57 | 1 | $links['visitors'] = [ |
| 58 | 1 | '#theme' => 'links__node__visitors', |
| 59 | 1 | '#links' => $statistics_links, |
| 60 | 1 | '#attributes' => [ |
| 61 | 1 | 'class' => [ |
| 62 | 1 | 'links', |
| 63 | 1 | 'inline', |
| 64 | 1 | ], |
| 65 | 1 | ], |
| 66 | 1 | ]; |
| 67 | } | |
| 68 | 1 | $links['#cache']['max-age'] = $settings->get('counter.display_max_age'); |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Implements hook_entity_delete(). | |
| 73 | */ | |
| 74 | #[Hook('entity_delete')] | |
| 75 | public function entityDelete(EntityInterface $entity) { | |
| 76 | 2 | $entity_id = $entity->id(); |
| 77 | 2 | if (!is_numeric($entity_id)) { |
| 78 | 1 | return; |
| 79 | } | |
| 80 | 1 | $entity_type = $entity->getEntityTypeId(); |
| 81 | 1 | $this->counter->deleteViews($entity_type, (int) $entity_id); |
| 82 | } | |
| 83 | ||
| 84 | } |