function 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 {
    $path = $this->routeMatch
        ->getRouteObject()
        ->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.