'Node from URL', but for the rest of us.

This project provides a service and context to determine if the current route match is owned by a particular entity type, by way of link templates.

A context is also provided for plugins and other to consume. For example you may want to create a block, or DsField, that relies on a particular entity from the route. It operates similar to the node.node_route_context context, but in an entity type agnostic way. Entity type specific contexts are also available in case a plugin is designed to take any entity type, but as a site builder you may choose for it to respond to a particular entity type.

Ideally this feature would be added to core in #3138465: Add a generic entity route context

Plugin context example

For example you may have the following block definition:

/**
 * @Block(
 *   id = "test_block",
 *   admin_label = @Translation("Test block"),
 *   context_definitions = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Entity"),
 *       required = TRUE
 *     )
 *   }
 * )
 */

In this case, the generic entity context and entity-type specific contexts will be made available for configuration. See screenshot.

If you choose the generic entity context then if you are on any entity type route then the entity will be provided to the block. If you choose an entity type context such as 'Content from route' (aka Node) then the block will only display if you are on a node.

If a block is designed to work with only a specific entity type, e.g nodes, then you can provide a definition such as:

/**
 * @Block(
 *   id = "test_block",
 *   admin_label = @Translation("Test block"),
 *   context_definitions = {
 *     "entity" = @ContextDefinition("entity:node",
 *       label = @Translation("Entity"),
 *       required = TRUE
 *     )
 *   }
 * )
 */

In this case, only the generic entity context and the 'Content from route' context will be made available. And each will only respond on Node routes. See screenshot.

V4 is compatible with Drupal 10.x, and is compatible with previous major versions.
V3 is compatible with Drupal 9.x, and is compatible with previous major versions.

Supporting organizations: 

Project information

Releases