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.

loze’s picture

Rebased !13 onto 2.0.x and added tests.

The idea is unchanged: instead of a hard-coded list of route parameters, the condition takes the first entity it finds in the route and checks the terms that entity references. Since the branch was last updated, #3217275: Add Media As Route Match added media to that list; with this change the list goes away, so no entity type needs adding by hand.

Two things worth pointing out:

  • A taxonomy term page now counts the term itself as referenced. Before, only the term's own references were checked, so the condition could never match on the page of the term it was configured with.
  • That goes through the same matching as everything else, so "Require all terms" from #3243787: Require availability of all terms for evaluation to be true applies to term pages too.

Tests. New TermConditionEntityTypesTest covers the condition on node, taxonomy term (the term itself and a term reached through parent), media and user routes, plus the require all case on a term page. The existing tests are untouched and pass.

The rebase and the tests were written with AI assistance. I have reviewed them and CI passes.