You are here

protected function RequestSubscriber::checkContentEntityCanonicalRoute in Content Translation Redirect 8

Check that the current route is the content entity canonical route.

Return value

\Drupal\Core\Entity\ContentEntityInterface|bool The entity instance. FALSE if no entity is matched.

1 call to RequestSubscriber::checkContentEntityCanonicalRoute()
RequestSubscriber::onRequestCheckRedirect in src/EventSubscriber/RequestSubscriber.php
Handles the redirect if any found.

File

src/EventSubscriber/RequestSubscriber.php, line 139

Class

RequestSubscriber
Redirect subscriber for controller requests.

Namespace

Drupal\content_translation_redirect\EventSubscriber

Code

protected function checkContentEntityCanonicalRoute() {

  // Get current route name from route match.
  $route_name = $this->routeMatch
    ->getRouteName();

  // If no route is matched.
  if (empty($route_name)) {
    return FALSE;
  }

  // Trying to find the Entity Type ID in the route parameters.
  foreach ($this->routeMatch
    ->getParameters() as $parameter) {
    if ($parameter instanceof ContentEntityInterface) {
      try {

        // Compare entity canonical URL with current URL.
        $entity_route_name = $parameter
          ->toUrl()
          ->getRouteName();
        if ($entity_route_name == $route_name) {
          return $parameter;
        }
      } catch (\Exception $e) {

        // There is no canonical URL for this entity,
        // proceed to the next entity.
        continue;
      }
    }
  }
  return FALSE;
}