Problem/Motivation
Primary Entity Reference currently provides only minimal Views integration. While the field works correctly in
entity forms and renderers, Views cannot:
- Create relationships to the referenced entities
- Filter or sort based on the primary reference flag
- Expose only the primary referenced entity in listings
- Match the functionality that core Entity Reference provides in Views
This makes it difficult to use the field in practical reporting and listing scenarios, particularly when a consumer
needs to join on, or specifically display, the primary referenced entity.
Steps to reproduce
- Create a content type that uses a Primary Entity Reference field.
- Attempt to add the field to a View as a relationship or contextual filter.
- Only minimal field display is available; no relationships or primary-only capabilities exist.
Proposed resolution
Implement full Views integration equivalent to core Entity Reference, using modern
service-based Views data providers and auto-wired Hook Attributes.
Instead of a primary_entity_reference.views.inc file or procedural hook_views_data(),
the module will register and expose its Views data through attribute-driven services.
The implementation would include:
- Views data service
- Provide a
views.views_dataservice for Primary Entity Reference. - Use Hook Attributes (e.g.,
#[ViewsData]) to auto-register Views data. - Expose field, filter, sort, and relationship metadata programmatically.
- Provide a
- Field handler plugin extending core ER field handler:
- Option: Display all referenced entities
- Option: Display only the primary referenced entity
- Relationship plugins:
- A standard ER-style relationship matching core behavior.
- A primary-only relationship applying a
primary = 1join condition.
- Filter plugin:
- Filtering by referenced entity with optional “primary only” mode.
- Sort plugin:
- Sorting by weight or by the primary flag.
Using service discovery and Hook Attributes ensures the module follows modern Drupal patterns and avoids the legacy
.views.inc approach entirely.
Remaining tasks
- Implement Views data provider service with Hook Attributes.
- Add field, filter, sort, and relationship plugins.
- Add primary-only relationship logic.
- Add tests covering each Views integration point.
- Document config options and usage examples.
User interface changes
- In the field handler options, add a setting to display:
- All referenced entities
- Primary reference only
API changes
- New Views plugins (field, filter, sort, relationship) under
Plugin\views\*. - New
views.views_dataservice using Hook Attributes.
Data model changes
No data model changes. The existing primary column already supports the necessary behavior.
Issue fork primary_entity_reference-3558094
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
Comment #2
bluegeek9 commentedComment #3
bluegeek9 commentedComment #5
bluegeek9 commentedComment #7
bluegeek9 commented