class NodeViewSubscriber in Open Social 8.8
Same name and namespace in other branches
- 8.9 modules/custom/social_node_statistics/src/EventSubscriber/NodeViewSubscriber.php \Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber
- 8.4 modules/custom/social_node_statistics/src/EventSubscriber/NodeViewSubscriber.php \Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber
- 8.5 modules/custom/social_node_statistics/src/EventSubscriber/NodeViewSubscriber.php \Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber
- 8.6 modules/custom/social_node_statistics/src/EventSubscriber/NodeViewSubscriber.php \Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber
- 8.7 modules/custom/social_node_statistics/src/EventSubscriber/NodeViewSubscriber.php \Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber
Class NodeViewSubscriber.
@package Drupal\social_node_statistics\EventSubscriber
Hierarchy
- class \Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of NodeViewSubscriber
1 string reference to 'NodeViewSubscriber'
- social_node_statistics.services.yml in modules/
custom/ social_node_statistics/ social_node_statistics.services.yml - modules/custom/social_node_statistics/social_node_statistics.services.yml
1 service uses NodeViewSubscriber
- social_node_statistics.nodeview_subscriber in modules/
custom/ social_node_statistics/ social_node_statistics.services.yml - Drupal\social_node_statistics\EventSubscriber\NodeViewSubscriber
File
- modules/
custom/ social_node_statistics/ src/ EventSubscriber/ NodeViewSubscriber.php, line 20
Namespace
Drupal\social_node_statistics\EventSubscriberView source
class NodeViewSubscriber implements EventSubscriberInterface {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The user account.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* The route matcher.
*
* @var \Symfony\Cmf\Component\Routing\NestedMatcher\NestedMatcher
*/
protected $routeMatcher;
/**
* The time.
*
* @var \Drupal\Component\Datetime\Time|TimeInterface
*/
protected $time;
/**
* Constructor.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
* @param \Drupal\Core\Session\AccountProxyInterface $account
* The user account.
* @param \Drupal\Core\Routing\CurrentRouteMatch $matcher
* The route matcher.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The system time.
*/
public function __construct(Connection $database, AccountProxyInterface $account, CurrentRouteMatch $matcher, TimeInterface $time) {
$this->database = $database;
$this->account = $account;
$this->routeMatcher = $matcher;
$this->time = $time;
}
/**
* Get the request events.
*
* @return mixed
* Returns request events.
*/
public static function getSubscribedEvents() {
$events[KernelEvents::TERMINATE][] = [
'trackNodeView',
];
return $events;
}
/**
* This method is called when the KernelEvents::TERMINATE event is dispatched.
*
* @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
* The event.
*
* @throws \Exception
*/
public function trackNodeView(PostResponseEvent $event) {
// If the response is a success (e.g. status code 200) we can proceed.
if ($event
->getResponse()
->isSuccessful()) {
// Check if we're on a node page.
if ($this->routeMatcher
->getRouteName() === 'entity.node.canonical') {
$node = $this->routeMatcher
->getParameter('node');
if ($node instanceof NodeInterface) {
$config = \Drupal::config('social_node_statistics.settings');
// Check if we should log for this node bundle.
if (in_array($node
->bundle(), $config
->get('node_types'), FALSE)) {
$now = $this->time
->getRequestTime();
// Insert the event in the table.
$this->database
->insert('node_statistics')
->fields([
'uid' => $this->account
->id(),
'nid' => $node
->id(),
'timestamp' => $now,
])
->execute();
// Get latest timestamp for possible cache invalidation.
$latest = $this->database
->select('node_statistics', 'n')
->fields('n', [
'timestamp',
])
->condition('n.nid', $node
->id())
->orderBy('n.timestamp', 'DESC')
->range('1', '1')
->execute()
->fetchField();
// Get count of views for possible cache invalidation.
$count = $this->database
->select('node_statistics', 'n')
->condition('n.nid', $node
->id())
->countQuery()
->execute()
->fetchField();
// Clear render cache only if max age is reached and the count is of
// a certain predefined number.
if ($now - $latest >= $config
->get('max_age') && $count % $config
->get('min_views') === 0) {
Cache::invalidateTags([
'node:' . $node
->id() . ':views_count',
]);
}
}
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
NodeViewSubscriber:: |
protected | property | The user account. | |
NodeViewSubscriber:: |
protected | property | The database connection. | |
NodeViewSubscriber:: |
protected | property | The route matcher. | |
NodeViewSubscriber:: |
protected | property | The time. | |
NodeViewSubscriber:: |
public static | function | Get the request events. | |
NodeViewSubscriber:: |
public | function | This method is called when the KernelEvents::TERMINATE event is dispatched. | |
NodeViewSubscriber:: |
public | function | Constructor. |