Follow-up of #3529125: Add content display overrides

EntityViewDisplay::getDisplayInfos (static, public, not covered by any interface, with a weak and confusing array structure) must be replaced by a proper use of Drupal services..

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

pdureau created an issue. See original summary.

pdureau’s picture

Issue tags: +Novice

Can we use EntityViewDisplay::collectRenderDisplays()?

::getDisplayInfos() returns an array of display information keyed with entity type, then 'modes' and 'bundles'. Example

[
  'node' => [
    'modes' => [
      'teaser' => 'Teaser',
    ],
   'bundles' => [
     'article' => [
       'teaser' => 'Teaser',
     ],
   ],
 ],
];

::collectRenderDisplay() returns \Drupal\Core\Entity\Display\EntityViewDisplayInterface[] the display objects to use to render the entities, keyed by entity bundle.

So it seems we have the same info.

gavinrfitzgerald made their first commit to this issue’s fork.

mehak22’s picture

Thanks for the issue and the detailed explanation.

From the discussion, it seems that EntityViewDisplay::getDisplayInfos() could be replaced with a more structured approach using services, and possibly EntityViewDisplay::collectRenderDisplays().

I would like to work on this by exploring how getDisplayInfos() is currently used and attempting to refactor it to use collectRenderDisplays() or an appropriate service-based solution.

Please let me know if there are any specific constraints or preferred approaches before I proceed.

Thanks!

pdureau’s picture

I would like to work on this by exploring how getDisplayInfos() is currently used and attempting to refactor it to use collectRenderDisplays() or an appropriate service-based solution.

It was just a guess on my side, it would be nice if it is confirmed to you.

The idea is to use as much as possible the interface we are already implementing instead of creating or extending custom interfaces.

mehak22’s picture

@pdureau
Thanks for the clarification.

Understood — I will verify whether collectRenderDisplays() can fully replace getDisplayInfos() in this context instead of assuming it directly.

I’ll start by reviewing how getDisplayInfos() is currently used and compare the data structure with what is returned by the existing interfaces, and then attempt a refactor using the appropriate entity display APIs.

I’ll share my findings or a proposed approach shortly.

Thanks!

mehak22’s picture

Hi,

I have worked on replacing the usage of EntityViewDisplay::getDisplayInfos() with a service-based approach using the entity_type.manager.

Specifically, I am loading entity view displays via:

$this->entityTypeManager()
->getStorage('entity_view_display')
->loadByProperties([
'targetEntityType' => $entity->getEntityTypeId(),
'bundle' => $entity->bundle(),
]);

From these, I derive the available view modes instead of relying on the static helper.

I have updated the relevant controller, routing, and derivative plugin usage accordingly.

I will proceed to create a merge request for review shortly.

Please let me know if there are any preferred approaches or adjustments needed.

Thanks!

mehak22’s picture

Hi,

I explored replacing `EntityViewDisplay::getDisplayInfos()` with a service-based approach using `entity_view_display` storage.

I have updated the relevant usages to load display configurations via the entity type manager and derive view modes from there.

Due to local environment limitations, I wasn’t able to complete the merge request workflow, so I am attaching a patch for review.

Please let me know if any changes or improvements are needed.

Thanks!

teebeecoder’s picture

Thank you @Mehak, I'll create the merge request and do the review.

teebeecoder’s picture

Assigned: Unassigned » teebeecoder
Status: Active » Needs review
pdureau’s picture

Assigned: teebeecoder » Unassigned
Status: Needs review » Needs work

There are a few issues with https://www.drupal.org/files/issues/2026-03-19/0001-Issue-3542273-Replac... including;

  • EntityViewDisplay::getDisplayInfos() has not been removed
  • OverridesRoutes has been totally replaced by a copy of EntityViewOverridesController

So, it is too soon to make a MR from the patch but I have opened a MR anyway as a starting point.

pdureau’s picture

We will be able to do this change after the release of 1.0.0 without breaking our APIs so, as the ticket author, I am proposing to remove it from 1.0.0 scope.

pdureau’s picture

Title: Remove EntityViewDisplay::getDisplayInfos » Remove EntityViewOverride::getDisplayInfos

The method has been moved to EntityViewOverride

pdureau’s picture

Status: Needs work » Needs review

Proposal to review.

  • In EntityViewOverridesController: replaced by an entity query (which add a bit of logic)
  • In EntityOverrideViewLocalTask: replaced by an entity query (which simplify the logic by removing a nested loop)
  • In OverridesRoutes: replaced by an entity query (which simplify the logic by removing a nested loop)

So, we have added a bit of code duplication, but the overall logic is simplified and the API is more tight.

No test has been modified 👍

One playwright job fail but it doesn't seem related to the change.

mogtofu33’s picture

Status: Needs review » Fixed

Thanks, tested and merged.
It removes a public method, so it goes in the RC1 change record. RTBC.

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.

  • mogtofu33 committed 65416bb3 on 1.0.x authored by pdureau
    task: #3542273 Remove EntityViewOverride::getDisplayInfos
    
    By: pdureau...
pdureau’s picture

It removes a public method, so it goes in the RC1 change record. RTBC.

OK :)

Status: Fixed » Closed (fixed)

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