Skip to content

Commit 3f3f60d

Browse files
committed
Changing logging logic for logged in users
1 parent c739d2c commit 3f3f60d

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
lines changed

os2web_logging.module

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,11 @@
77

88
use Drupal\Component\Datetime\DateTimePlus;
99
use Drupal\Core\Access\AccessResult;
10-
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
11-
use Drupal\Core\Entity\EntityInterface;
1210
use Drupal\Core\Form\FormStateInterface;
1311
use Drupal\Core\Session\AccountInterface;
1412
use Drupal\os2web_logging\Controller\LoggingController;
1513
use Drupal\os2web_logging\Form\SettingsForm;
1614

17-
/**
18-
* Implements hook_ENTITY_TYPE_view().
19-
*/
20-
function os2web_logging_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
21-
// Ignoring requests from CLI.
22-
if (PHP_SAPI === 'cli') {
23-
return;
24-
}
25-
26-
$config = \Drupal::config(SettingsForm::$configName);
27-
28-
// Ignore anonymous user.
29-
$logAnonymUser = $config->get('log_anonymous_user');
30-
if (!$logAnonymUser && \Drupal::currentUser()->isAnonymous()) {
31-
return;
32-
}
33-
34-
// Filter on node type.
35-
if ($node_types = $config->get('logged_node_types')) {
36-
if (in_array($entity->getType(), $node_types, TRUE)) {
37-
38-
// Log entry.
39-
$logger = \Drupal::logger('os2web_logging.access_log');
40-
$logger->info('Node loaded', ['sid' => $entity->id()]);
41-
}
42-
}
43-
}
44-
4515
/**
4616
* Implements hook_webform_element_access().
4717
*/

os2web_logging.services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ services:
1919
class: Drupal\os2web_logging\Logger\AccessLogsDbLog
2020
tags:
2121
- { name: logger }
22+
os2web_logging.node_access_event_subscriber:
23+
class: Drupal\os2web_logging\EventSubscriber\NodeAccessEventSubscriber
24+
arguments: []
25+
tags:
26+
- {name: event_subscriber}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Drupal\os2web_logging\EventSubscriber;
4+
5+
use Drupal\os2web_logging\Form\SettingsForm;
6+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
use Symfony\Component\HttpKernel\KernelEvents;
8+
9+
/**
10+
* Class NodeAccessEventSubscriber.
11+
*
12+
* Runs on every request.
13+
*/
14+
class NodeAccessEventSubscriber implements EventSubscriberInterface {
15+
16+
/**
17+
* Logs node access.
18+
*/
19+
public function logNodeAccess() {
20+
/** @var Drupal\node\NodeInterface $node */
21+
$node = NULL;
22+
23+
$routeMatch = \Drupal::routeMatch();
24+
if ($routeMatch->getRouteName() == 'entity.node.canonical' && $node = $routeMatch->getParameter('node')) {
25+
$config = \Drupal::config(SettingsForm::$configName);
26+
27+
// Ignore anonymous user.
28+
$logAnonymUser = $config->get('log_anonymous_user');
29+
if (!$logAnonymUser && \Drupal::currentUser()->isAnonymous()) {
30+
return;
31+
}
32+
33+
// Filter on node type.
34+
if ($node_types = $config->get('logged_node_types')) {
35+
if (in_array($node->getType(), $node_types, TRUE)) {
36+
37+
// Log entry.
38+
$logger = \Drupal::logger('os2web_logging.access_log');
39+
$logger->info('Node loaded', ['sid' => $node->id()]);
40+
}
41+
}
42+
}
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public static function getSubscribedEvents() {
49+
$events[KernelEvents::REQUEST][] = ['logNodeAccess'];
50+
return $events;
51+
}
52+
53+
}

0 commit comments

Comments
 (0)