function EntityRouteHelper::getContentEntityFromRoute

Same name and namespace in other branches
  1. 11.x core/modules/navigation/src/EntityRouteHelper.php \Drupal\navigation\EntityRouteHelper::getContentEntityFromRoute()

File

core/modules/navigation/src/EntityRouteHelper.php, line 57

Class

EntityRouteHelper
Helper service to detect entity routes.

Namespace

Drupal\navigation

Code

public function getContentEntityFromRoute() : ?ContentEntityInterface {
  $route = $this->routeMatch
    ->getRouteObject();
  if (!$route) {
    return NULL;
  }
  $path = $route->getPath();
  if (!$entity_type = $this->getContentEntityPaths()[$path] ?? NULL) {
    return NULL;
  }
  $entity = $this->routeMatch
    ->getParameter($entity_type);
  if ($entity instanceof ContentEntityInterface && $entity->getEntityTypeId() === $entity_type) {
    return $entity;
  }
  return NULL;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.