You are here

class RngEventRouteContext in RNG - Events and Registrations 8

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

Get the current RNG event from the route.

Hierarchy

Expanded class hierarchy of RngEventRouteContext

1 string reference to 'RngEventRouteContext'
rng.services.yml in ./rng.services.yml
rng.services.yml
1 service uses RngEventRouteContext
rng.event_route_context in ./rng.services.yml
Drupal\rng\ContextProvider\RngEventRouteContext

File

src/ContextProvider/RngEventRouteContext.php, line 17

Namespace

Drupal\rng\ContextProvider
View source
class RngEventRouteContext implements ContextProviderInterface {
  use StringTranslationTrait;

  /**
   * The route match object.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The RNG event manager.
   *
   * @var \Drupal\rng\EventManagerInterface
   */
  protected $eventManager;

  /**
   * Constructs a new RngEventRouteContext.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match object.
   * @param \Drupal\rng\EventManagerInterface $event_manager
   *   The RNG event manager.
   */
  public function __construct(RouteMatchInterface $route_match, EventManagerInterface $event_manager) {
    $this->routeMatch = $route_match;
    $this->eventManager = $event_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    $cacheability = new CacheableMetadata();
    $cacheability
      ->setCacheContexts([
      'rng_event',
    ]);
    $context_definition = new ContextDefinition('entity', NULL, FALSE);
    $context = new Context($context_definition, $this
      ->getEventInRoute());
    $context
      ->addCacheableDependency($cacheability);
    return [
      'rng_event' => $context,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableContexts() {
    $context = new Context(new ContextDefinition('entity', $this
      ->t('RNG event from route')));
    return [
      'rng_event' => $context,
    ];
  }

  /**
   * Determine the event in the current route.
   *
   * @return \Drupal\Core\Entity\EntityInterface|NULL
   *   The event entity, or NULL.
   */
  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;
  }

  /**
   * Get all event entities from the current route.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   An array of event entities in route.
   */
  protected function getEventEntitiesInRoute() {
    $events = [];
    if (($route = $this->routeMatch
      ->getRouteObject()) && ($parameters = $route
      ->getOption('parameters'))) {
      foreach ($parameters as $parameter) {
        if (isset($parameter['type']) && strpos($parameter['type'], 'entity:') !== FALSE) {
          $entity_type_id = substr($parameter['type'], strlen('entity:'));
          $entity = $this->routeMatch
            ->getParameter($entity_type_id);
          if ($entity instanceof EntityInterface && $this->eventManager
            ->isEvent($entity)) {
            $events[] = $entity;
          }
        }
      }
    }
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RngEventRouteContext::$eventManager protected property The RNG event manager.
RngEventRouteContext::$routeMatch protected property The route match object.
RngEventRouteContext::getAvailableContexts public function Gets all available contexts for the purposes of configuration. Overrides ContextProviderInterface::getAvailableContexts
RngEventRouteContext::getEventEntitiesInRoute protected function Get all event entities from the current route.
RngEventRouteContext::getEventInRoute protected function Determine the event in the current route.
RngEventRouteContext::getRuntimeContexts public function Gets runtime context values for the given context IDs. Overrides ContextProviderInterface::getRuntimeContexts
RngEventRouteContext::__construct public function Constructs a new RngEventRouteContext.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.