Cleanups identified while reviewing
#3615031.
All pre-existing, none blocking that issue. Grouped because the first two touch
the same interface.

1. Add getSettingsUrl(): ?Url to DisplayBuildableInterface

"Where is this display configured, if that page exists" is implemented three
times, each composing a route and guarding it with
RouteNotFoundException:

  • EntityView::manageDisplayUrl() returns NULL
  • ViewDisplay::viewEditUrl() returns
    Url::fromRoute('entity.display_builder_instance.collection')
  • ContextualFormPanel::settingsUrl() returns NULL

They do not merely duplicate each other, they disagree: three call sites,
three different answers to "there is no such page". All three answer a question
the buildable should answer about itself, and all three already feed
DisplayReference::$settingsUrl from collectDisplays().
An instance-level getSettingsUrl(): ?Url gives one implementation
per plugin and one meaning for absence.

It also removes a null object. getDisplayUrlFromInstanceId() is
contractually non-nullable, so
EntityViewOverride::getDisplayUrlFromInstanceId() returns
Url::fromRoute('<front>) purely to satisfy a signature that
should have allowed NULL.

2. Add DisplayBuildablePluginManager::getDefinitionForInstanceId()

Three callers scan plugin definitions for a matching
instance_prefix, each hand-rolling the same loop:

  • InstanceAccessControlHandler
  • Event\PageVariantSubscriber
  • Plugin\display_builder\Island\BackButton

One manager method replaces all three. It would also let
DisplayBuildablePluginBase::getPrefix() read
$this->pluginDefinition['instance_prefix'] instead of reflecting
over the plugin attribute, dropping the reflection and the per-class
memoization it needs.

3. Fold the remaining copies of view-mode naming

Two places still load entity_view_mode config entities by hand
to name a view mode:

  • EntityViewOverride::getDisplayInfos(), twice on adjacent
    lines, with its own hard-coded default fallback
  • Form\EntityViewDisplayFormTrait

#3615031 moved the canonical version into
EntityDisplayLabelTrait and switched it to
entity_display.repository, which is cached per entity type and
honors hook_entity_view_mode_info_alter(). That service works from
a static context, which is what makes getDisplayInfos() foldable
now.

See #3616451: Instances listing builds a buildable plugin three times per row for the instances listing performance problem found in
the same pass.

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

mogtofu33 created an issue. See original summary.

mogtofu33’s picture

mogtofu33’s picture

Issue summary: View changes
mogtofu33’s picture

Status: Active » Needs work
mogtofu33’s picture

Assigned: mogtofu33 » Unassigned
Status: Needs work » Active
mogtofu33’s picture

mogtofu33’s picture

pdureau’s picture

Assigned: Unassigned » pdureau

This issue may be outdated.

1. Add getSettingsUrl(): ?Url to DisplayBuildableInterface

This method is already added in #3616313: Check static methods on DisplayBuildableInterface (and maybe replaced by ::toUrl('edit-form '): ?Url)

So, let's skip this one.

2. Add DisplayBuildablePluginManager::getDefinitionForInstanceId()

BackButton now calls $builder->getBuildablePlugin(), like the access handler. I am afraid getDefinitionForInstanceId() would have no callers, and an Instance always knows its own plugin, which makes reading meaning into the ID unnecessary.

Also, the works done in #3616313: Check static methods on DisplayBuildableInterface will allow us to make ::getPrefix() an instance method instead of a static method, resolving this refection/memoization issue.

I will do it directly in the other ticket.

3. Fold the remaining copies of view-mode naming

EntityViewOverride::getDisplayInfos() will be removed hopefully (#3542273: Remove EntityViewOverride::getDisplayInfos ) but it is still here for now.

So, the idea would be to replace calls to $entityTypeManager->getStorage('entity_view_mode') by $entityDisplayRepository->getViewModeOptions()?

It worth a try. Without any change in phpunit and playwright tests, of course ;)

pdureau’s picture

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

$entityTypeManager->getStorage('entity_view_mode') has been replaced by $entityDisplayRepository->getViewModeOptions() in:

  • modules/display_builder_entity_view/src/Form/EntityViewDisplayFormTrait.php
  • modules/display_builder_entity_view/src/Plugin/Derivative/EntityOverrideViewLocalTask.php
  • (Indirectly) modules/display_builder_entity_view/src/Plugin/display_builder/Buildable/EntityViewOverride.php
smovs’s picture

Hi team! I've reviewed the MR on my local copy. A few things:

One request before RTBC:
The issue summary points at EntityDisplayLabelTrait::getViewModeLabel() as the canonical helper to fold the remaining copies into, but the MR inlines a getViewModeOptions() lookup in EntityViewDisplayFormTrait and again in EntityOverrideViewLocalTask. That leaves three copies of the same "options[$mode] ?? $mode" fallback in the submodule. The deriver already injects the repository and both form classes inherit it from EntityDisplayFormBase, so both can use the trait. The form trait only needs to map 'default' to 'full' before calling it; the helper already names the default mode. For an issue about collapsing duplicates, this seems worth doing here rather than in a follow-up.

Two small test nits, non-blocking:
- In testDefaultDisplayIsNamedAfterFullViewMode, the "if (!EntityViewMode::load(...))" guard is dead: EntityKernelTestBase installs config only for field, so the entity_test full view mode is never present. Creating it unconditionally reads clearer.
- testDefaultDisplayWithoutFullViewMode relies on the user module's config not being installed, but user does ship a full view mode. An explicit assertion that it is absent would make the precondition visible.

pdureau’s picture

Hi smovs,

Thanks for your review.

The issue summary points at EntityDisplayLabelTrait::getViewModeLabel() as the canonical helper to fold the remaining copies into, but the MR inlines a getViewModeOptions() lookup in EntityViewDisplayFormTrait and again in EntityOverrideViewLocalTask. That leaves three copies of the same "options[$mode] ?? $mode" fallback in the submodule.

I don't see any reference of EntityDisplayLabelTrait::getViewModeLabel() in the issue description.

It is me who proposed ::getViewModeOptions() and it may not be the best solution. You are warmly welcomed to assign the ticket to you in "Needs work" to propose something more streamlined.

smovs’s picture

Thanks @pdureau, you're right, and I was wrong in #13. The summary only says the canonical version is in EntityDisplayLabelTrait. It doesn't name getViewModeLabel().

I went through the MR again more carefully, with Claude's help this time, and my suggestion doesn't work:

  • The forms can't use the trait. Core's EntityDisplayFormBase already has protected $entityDisplayRepository; without a type. The trait declares it with a type, and PHP stops with a fatal error (we checked this with a small PHP repro of the same class layout).
  • The form's lookup isn't really a copy. It tries full first, then default. The helper doesn't do that.

So I'm withdrawing that request. The code is fine as it is.

Two small test suggestions, both optional:

1. In testDefaultDisplayIsNamedAfterFullViewMode(), the if is always true, because this kernel test never installs the entity_test.full view mode:

-    if (!EntityViewMode::load('entity_test.full')) {
-      EntityViewMode::create([
-        'id' => 'entity_test.full',
-        'label' => 'Full content',
-        'targetEntityType' => 'entity_test',
-      ])->save();
-    }
+    EntityViewMode::create([
+      'id' => 'entity_test.full',
+      'label' => 'Full content',
+      'targetEntityType' => 'entity_test',
+    ])->save();

2. In testDefaultDisplayWithoutFullViewMode(), the test depends on user.full not existing. The user module ships that view mode, so asserting it's absent makes the assumption visible:

   public function testDefaultDisplayWithoutFullViewMode(): void {
+    self::assertNull(EntityViewMode::load('user.full'));
     self::assertSame('Default display override', $this->createOverrideFieldLabel('user', 'user'));
   }

RTBC from me, with or without these.

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
Status: Needs review » Reviewed & tested by the community

✅ Tested and works — applied MR !509, rebuilt caches and created the display override field for the default display of menu_link_content (no full view mode) through EntityViewDisplayForm::createOverrideField(): the field is now labelled "Default display override" instead of failing. The view-mode naming fold also fixes this crash. Suggest to RTBC

Before
$ ddev drush php:script repro-3616450.php
Error: Call to a member function label() on null
After
$ ddev drush php:script repro-3616450.php
Label: Default display override
Notes
  • Node local task titles are unchanged after the fix: "Display: Full content", "Display: Teaser".
  • Code review: MR !509 at 4f85b3f8, merge-result pipeline green; the new kernel test covers the crash path, no blocking findings.
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
pdureau’s picture

Assigned: Unassigned » pdureau

Rebasing because EntityViewDisplayFormTrait has been removed by #3594228: Remove explicit references to Layout Builder

pdureau’s picture

Assigned: pdureau » mogtofu33

The rebase has been heavy and risky, because of the removal of EntityViewDisplayFormTrait in main branch.

However, I am deciding to keep it in RTBC, because:

  • the changes in EntityViewDisplayFormHook follow the ones previously made in EntityViewDisplayFormTrait
  • the changes in EntityViewDisplayFormOverrideFieldTest frollow the switch from EntityViewDisplayFormTraitto EntityViewDisplayFormHook
  • EntityOverrideViewLocalTask and EntityViewOverride are identical

RTBC version from last week:
4f85b3f8b4060f1b6cafee4496af830d2da51dd0:

 modules/display_builder_entity_view/src/Form/EntityViewDisplayFormTrait.php                     | 11 ++++++-----
 modules/display_builder_entity_view/src/Plugin/Derivative/EntityOverrideViewLocalTask.php       | 11 ++++++++---
 modules/display_builder_entity_view/src/Plugin/display_builder/Buildable/EntityViewOverride.php | 14 +++++++-------
 modules/display_builder_entity_view/tests/src/Kernel/EntityViewDisplayFormOverrideFieldTest.php | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 117 insertions(+), 15 deletions(-)

I hope it is OK for you.

Pushed today:
9ed6c6ea608d26b471dc7ca5fdc36a43c6d5504b:

 modules/display_builder_entity_view/src/Hook/EntityViewDisplayFormHook.php                      | 13 ++++++++-----
 modules/display_builder_entity_view/src/Plugin/Derivative/EntityOverrideViewLocalTask.php       | 11 ++++++++---
 modules/display_builder_entity_view/src/Plugin/display_builder/Buildable/EntityViewOverride.php | 14 +++++++-------
 modules/display_builder_entity_view/tests/src/Kernel/EntityViewDisplayFormOverrideFieldTest.php | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 117 insertions(+), 15 deletions(-)
mogtofu33’s picture

Title: Collapse the duplicated "where is this display configured" helpers and the prefix scans » Fold the remaining copies of view-mode naming,
Assigned: mogtofu33 » Unassigned
Status: Reviewed & tested by the community » Fixed

Thanks.

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 5b4a4d47 on 1.0.x authored by pdureau
    task: #3616450 Fold the remaining copies of view-mode naming,
    
    By:...
pdureau’s picture

Status: Fixed » Closed (fixed)

Shipped in beta10 release.