Entity reference Computed Token Fields should behave like regular reference fields

Created on 26 March 2025, 14 days ago

Problem/Motivation

If we create a Computed Token: Entityreference field, and use a token that points to the target_id of a source field referencing a User (or other entity), the Computed Token field will successfully render the user (or other entity) it references, but it the field doesn't behave *exactly* like a regular Entity Reference field that's configured to reference an entity.

In particular, such a Computed Token entityreference field, when exposed to Views, will not be recognized as a reference to the target entity, and thus filters that rely on the User (or other entity) such as "User (autocomplete)" will not be available, because Views only sees this field as a generic "entityreference" field, rather than a "userreference".

Steps to reproduce

  1. Create a node type with a User reference field (field_user_ref)
  2. Add a Computed Token: Entity reference field (field_computed_user_ref) and configure it with a token to point to field_user_ref's value ([node:field_user_ref:entity:target_id])
  3. Create a node of this type, and fill in the field_user_ref field.
  4. Create a View that is a simple listing of nodes.
  5. Add the field_user_ref and field_computed_user_ref fields to the View Fields
  6. Add a "User (autocomplete)" filter for the field_user_ref field
  7. Observe that no such "User (autocomplete)" filter is available for the field_computed_user_ref field

Proposed resolution

Identify the key difference(s) between a normal User Reference field and the Computed Token variant, and implement whatever is required to allow Views to recognize the underlying entity behind the reference.

Remaining tasks

Build a test case
Identify the differences
Fix them

User interface changes

TBD

API changes

TBD

Data model changes

TBD

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇨🇦Canada spiderman Halifax, NS

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @spiderman
  • 🇨🇦Canada spiderman Halifax, NS
  • 🇨🇦Canada spiderman Halifax, NS

    I've been puzzling over this for awhile today, and as best I can tell this is really a Views-specific thing that we're missing, to wire up the Computed Token entityreference fields into the views_data_alter $data array correctly. I've managed to narrow down the core behaviour somewhat, and I believe the core views.views.inc is the key piece that adds the "X referenced by Y" relationships for entity_reference fields, and figures out which data table to join in based on the target_type of the field. What I can't see is why our Computed Token Field's aren't being recognized and included in there- we're obviously missing something, but I'm not sure what it is.

    Possible we need to manually "wire in" the Computed Token fields in our existing computed_token_field.views.inc, which we've done for datetime fields. But I don't really understand what the shape of the $data array we should provide is. Alternately, perhaps our FieldType plugin just needs to implement some Interface it's not currently, or have an extra bit added to its Entity annotation, and it'll Just Work(tm). Any insights welcome!

    • spiderman committed 13468958 on 1.0.x
      Issue #3515534: Entity reference Computed Token Fields should behave...
  • 🇨🇦Canada spiderman Halifax, NS

    Success! I'm pretty sure I got to the bottom of this, once I found that UserViewsData was providing the filter, and started tracing backward to the relationship that core entityreference fields end up getting in the ViewsData array. Eventually I landed on core_field_views_data() in views.views.inc (which looks like it's going away in D11?), and figured out that our computed token entity reference fields were not being handled by this core method (due to our "provider" not being "core", and a slight difference in the "target_type").

    As such, I've pushed a patch that copy/pastes the same logic from core_field_views_data into our computed_token_field_views_data_alter() implementation, and it seems to do the trick. I'll do some further testing in situ, but it looks like this should do the tricky. Yay!

  • 🇨🇦Canada spiderman Halifax, NS
  • 🇨🇦Canada spiderman Halifax, NS
Production build 0.71.5 2024