class EntityViewEventSubscriber in Better Statistics 8
Class EntityViewEventSubscriber.
@package Drupal\better_statistics
Hierarchy
- class \Drupal\better_statistics\EventSubscriber\EntityViewEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of EntityViewEventSubscriber
1 string reference to 'EntityViewEventSubscriber'
1 service uses EntityViewEventSubscriber
File
- src/
EventSubscriber/ EntityViewEventSubscriber.php, line 17
Namespace
Drupal\better_statistics\EventSubscriberView source
class EntityViewEventSubscriber implements EventSubscriberInterface {
/**
* Defines type of action that can be used.
*
* @constant for what entity interaction "action" is used for view
*/
const ACTION_VIEW = "View";
/**
* Drupal\Core\Routing\CurrentRouteMatch definition.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $currentRouteMatch;
/**
* Drupal\Core\Session\AccountProxy definition.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* Drupal\better_statistics\.
*
* @var \Drupal\better_statistics\EntityInteractionCollector
*/
protected $entityInteractionCollector;
/**
* Constructor.
*/
public function __construct(CurrentRouteMatch $currentRouteMatch, AccountProxy $currentUser, EntityInteractionCollector $entityInteractionCollector) {
$this->currentRouteMatch = $currentRouteMatch;
$this->currentUser = $currentUser;
$this->entityInteractionCollector = $entityInteractionCollector;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Priority is set to 1 to avoid this listener from being stopped by other
// listeners.
// @see ContainerAwareEventDispatcher::dispatch()
$events[KernelEvents::VIEW][] = [
'onEventView',
1,
];
return $events;
}
/**
* Triggering method for entity view routes.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* Event that called action.
*/
public function onEventView(GetResponseForControllerResultEvent $event) {
try {
if (preg_match('/entity\\.([\\w]+)\\.canonical/', $this->currentRouteMatch
->getRouteName(), $matches)) {
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $this->currentRouteMatch
->getParameter($matches[1]);
// Pass the interaction to the collector.
$this->entityInteractionCollector
->actionTypeTrigger($entity, $this->currentUser, self::ACTION_VIEW);
}
} catch (\Exception $e) {
watchdog_exception('better_statistics', $e);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityViewEventSubscriber:: |
protected | property | Drupal\Core\Routing\CurrentRouteMatch definition. | |
EntityViewEventSubscriber:: |
protected | property | Drupal\Core\Session\AccountProxy definition. | |
EntityViewEventSubscriber:: |
protected | property | Drupal\better_statistics\. | |
EntityViewEventSubscriber:: |
constant | Defines type of action that can be used. | ||
EntityViewEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
EntityViewEventSubscriber:: |
public | function | Triggering method for entity view routes. | |
EntityViewEventSubscriber:: |
public | function | Constructor. |