Problem/Motivation

The term_condition module currently only supports nodes through its context definition. While there is fallback logic to handle a few other types (taxonomy_term, node_revision, node_preview), it's hardcoded to check specific entity types in route parameters. This limits the module's flexibility and requires updates every time a new entity type needs to be supported.
The code includes a comment acknowledging this limitation:

// Potential other ways to try fetch the entity. Assoc array to try get
// revisions. I wonder if there is a cleaner way to do this?
// @todo Provide hook to add extras.

The condition should work with any entity type (nodes, taxonomy terms, users, media, commerce products, etc.) without needing to hardcode each entity type.

Steps to reproduce

  1. Install and enable the term_condition module
  2. Create a taxonomy term condition
  3. Try to use the condition on entity types other than the hardcoded ones (e.g., user pages, media pages, commerce product pages)
  4. Observe that the condition doesn't evaluate properly

Proposed resolution

Refactor the entity detection logic to dynamically support any entity type:

  1. Keep the existing node context definition for backward compatibility and performance (fast path for most common case)
  2. Add a new getEntityFromRoute() method that:
    • Iterates through all route parameters dynamically
    • Detects any parameter that implements EntityInterface
    • Handles string parameters (entity IDs or revision IDs) by:
      • Checking if the parameter name is a valid entity type
      • Attempting to load revisions for revisionable entity types
      • Falling back to regular entity loading if needed
  3. Update evaluate() to:
    • First try the node context (backward compatible, fast path)
    • Fall back to getEntityFromRoute() for other entity types
    • Check if the entity itself is a taxonomy term (for term canonical pages)
    • Check entity references for taxonomy terms (for other entity types)

This approach:

  • Works with any entity type without hardcoding
  • Maintains backward compatibility
  • Handles entity IDs and revision IDs
  • Checks both revisionable and non-revisionable entity types
  • Supports both "entity is a term" and "entity references a term" scenarios
  • Addresses the @todo about providing a cleaner way to fetch entities

API Changes

None - this is backward compatible. The node context continues to work as before, with enhanced fallback logic for other entity types.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

loze created an issue. See original summary.

loze’s picture

MR13 gets this working for taxonomy_term entities.

loze’s picture

Status: Active » Needs review
loze’s picture

Title: Support taxonomy_term canonical pages » Support any entity type
Issue summary: View changes
Status: Needs review » Active
loze’s picture

Status: Active » Needs review

MR13 is ready for review. This should work with any route that has an entity in the route params.

It checks for node first, then uses the first entity found in the route params.

This eliminates the need to hardcode fallback entity types in the evaluate code.