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_data service for Primary Entity Reference.
    • Use Hook Attributes (e.g., #[ViewsData]) to auto-register Views data.
    • Expose field, filter, sort, and relationship metadata programmatically.
  • 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 = 1 join 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_data service using Hook Attributes.

Data model changes

No data model changes. The existing primary column already supports the necessary behavior.

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

jdleonard created an issue. See original summary.

bluegeek9’s picture

Priority: Normal » Major
Issue summary: View changes
Issue tags: +CRM Beta Blocker
bluegeek9’s picture

Assigned: Unassigned » bluegeek9

bluegeek9’s picture

Assigned: bluegeek9 » Unassigned
Status: Active » Needs review

bluegeek9’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.