You are here

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'
better_statistics.services.yml in ./better_statistics.services.yml
better_statistics.services.yml
1 service uses EntityViewEventSubscriber
better_statistics.entity_view_event_subscriber in ./better_statistics.services.yml
Drupal\better_statistics\EventSubscriber\EntityViewEventSubscriber

File

src/EventSubscriber/EntityViewEventSubscriber.php, line 17

Namespace

Drupal\better_statistics\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
EntityViewEventSubscriber::$currentRouteMatch protected property Drupal\Core\Routing\CurrentRouteMatch definition.
EntityViewEventSubscriber::$currentUser protected property Drupal\Core\Session\AccountProxy definition.
EntityViewEventSubscriber::$entityInteractionCollector protected property Drupal\better_statistics\.
EntityViewEventSubscriber::ACTION_VIEW constant Defines type of action that can be used.
EntityViewEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityViewEventSubscriber::onEventView public function Triggering method for entity view routes.
EntityViewEventSubscriber::__construct public function Constructor.