You are here

public function BehaviorInvoker::getEntity in Rabbit Hole 8

Same name and namespace in other branches
  1. 2.x src/BehaviorInvoker.php \Drupal\rabbit_hole\BehaviorInvoker::getEntity()

Retrieves entity to apply rabbit hole behavior from event object.

Parameters

\Symfony\Component\HttpKernel\Event\KernelEvent $event: The kernel request event.

Return value

\Drupal\Core\Entity\ContentEntityInterface|false Entity object if the Rabbit Hole action is applicable or FALSE otherwise.

Overrides BehaviorInvokerInterface::getEntity

File

src/BehaviorInvoker.php, line 100

Class

BehaviorInvoker
Default implementation of Rabbit Hole behaviors invoker.

Namespace

Drupal\rabbit_hole

Code

public function getEntity(KernelEvent $event) {
  $request = $event
    ->getRequest();

  // Don't process events with HTTP exceptions - those have either been thrown
  // by us or have nothing to do with rabbit hole.
  if ($request
    ->get('exception') != NULL) {
    return FALSE;
  }

  // Get the route from the request.
  if ($route = $request
    ->get('_route')) {

    // Only continue if the request route is the an entity canonical.
    if (preg_match('/^entity\\.(.+)\\.canonical$/', $route)) {

      // We check for all of our known entity keys that work with rabbit hole
      // and invoke rabbit hole behavior on the first one we find (which
      // should also be the only one).
      $entity_keys = $this
        ->getPossibleEntityTypeKeys();
      foreach ($entity_keys as $ekey) {
        $entity = $request
          ->get($ekey);
        if (isset($entity) && $entity instanceof ContentEntityInterface) {
          return $entity;
        }
      }
    }
  }
  return FALSE;
}