You are here

protected function RngEventRouteContext::getEventInRoute in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/ContextProvider/RngEventRouteContext.php \Drupal\rng\ContextProvider\RngEventRouteContext::getEventInRoute()
  2. 3.x src/ContextProvider/RngEventRouteContext.php \Drupal\rng\ContextProvider\RngEventRouteContext::getEventInRoute()

Determine the event in the current route.

Return value

\Drupal\Core\Entity\EntityInterface|NULL The event entity, or NULL.

1 call to RngEventRouteContext::getEventInRoute()
RngEventRouteContext::getRuntimeContexts in src/ContextProvider/RngEventRouteContext.php
Gets runtime context values for the given context IDs.

File

src/ContextProvider/RngEventRouteContext.php, line 77

Class

RngEventRouteContext
Get the current RNG event from the route.

Namespace

Drupal\rng\ContextProvider

Code

protected function getEventInRoute() {

  // Will be NULL in CLI.
  if (!($route = $this->routeMatch
    ->getRouteObject())) {
    return NULL;
  }
  if ($event_param = $route
    ->getDefault('event')) {
    $event = $this->routeMatch
      ->getParameter($event_param);
    return $event instanceof EntityInterface ? $event : NULL;
  }
  if ($events = $this
    ->getEventEntitiesInRoute()) {

    // There could be multiple events in a route, determine which event
    // is the correct one.
    foreach ($events as $entity) {

      // Exact link templates
      foreach ($entity
        ->getEntityType()
        ->getLinkTemplates() as $link_template => $path) {
        if ($route
          ->getPath() === $path) {
          return $entity;
        }
      }

      // Or this route is a sub string of canonical.
      if ($canonical_path = $entity
        ->getEntityType()
        ->getLinkTemplate('canonical')) {
        if (strpos($route
          ->getPath(), $canonical_path) === 0) {
          return $entity;
        }
      }
    }
  }
  return NULL;
}