Skip to content

CurrentEntity

Provides helper for working with current entities.


  • Full name: \Drupal\helper\CurrentEntity
  • This class implements: CacheableDependencyInterface

Properties

moduleHandler

protected \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler

routeMatch

protected \Drupal\Core\Routing\RouteMatchInterface $routeMatch

contextRepository

protected \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contextRepository

entityTypeManager

protected \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager

memoryCache

protected \Drupal\Core\Cache\CacheBackendInterface $memoryCache

Methods

__construct

CurrentEntity constructor.

public __construct(\Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler, \Drupal\Core\Routing\RouteMatchInterface $routeMatch, \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contextRepository, \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager, \Drupal\Core\Cache\CacheBackendInterface $memoryCache): mixed

Parameters:

Parameter Type Description
$moduleHandler \Drupal\Core\Extension\ModuleHandlerInterface
The module handler.
$routeMatch \Drupal\Core\Routing\RouteMatchInterface
Route match.
$contextRepository \Drupal\Core\Plugin\Context\ContextRepositoryInterface
The context repository.
$entityTypeManager \Drupal\Core\Entity\EntityTypeManagerInterface
The entity type manager.
$memoryCache \Drupal\Core\Cache\CacheBackendInterface
The memory cache.

fromRouteMatch

Gets the entity from a route match.

public fromRouteMatch(\Drupal\Core\Routing\RouteMatchInterface|null $routeMatch = null, string|null $matchRouteName = null): \Drupal\Core\Entity\EntityInterface|null

Parameters:

Parameter Type Description
$routeMatch \Drupal\Core\Routing\RouteMatchInterface|null
The route match to use, otherwise uses the current route match.
$matchRouteName string|null
Optional route name pattern to match against. If provided, the entity
will only be retrieved if the route name matches this pattern using
fnmatch.

Return Value:

The current page entity, or NULL if this is not an entity route.

See Also:

  • \Drupal\helper\metatag_get_route_entity()

fromRequest

Get the current entity from the request.

public fromRequest(\Symfony\Component\HttpFoundation\Request $request, string|null $matchRouteName = null): \Drupal\Core\Entity\EntityInterface|null

Parameters:

Parameter Type Description
$request \Symfony\Component\HttpFoundation\Request
The request object.
$matchRouteName string|null
Optional route name pattern to match against. If provided, the entity
will only be retrieved if the route name matches this pattern using
fnmatch.

Return Value:

The entity from the request if available, otherwise NULL.


fromRouteAndParameters

Gets the entity from a route and its parameters.

public fromRouteAndParameters(string $routeName, \Symfony\Component\HttpFoundation\ParameterBag $parameters): \Drupal\Core\Entity\EntityInterface|null

Parameters:

Parameter Type Description
$routeName string
The route name.
$parameters \Symfony\Component\HttpFoundation\ParameterBag
The route parameters.

Return Value:

The route's entity, or NULL if this is not an entity route.


fromLayoutBuilderContext

Gets the current layout builder entity.

public fromLayoutBuilderContext(): \Drupal\Core\Entity\EntityInterface|null

Return Value:

The current layout entity context.


fromContextId

Get the entity from a context ID.

public fromContextId(string $contextId): \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\EntityTypeInterface|null

Parameters:

Parameter Type Description
$contextId string

Return Value:

The context entity or entity type if available.

Throws:

If the context was not found. - ContextException