Problem/Motivation
It would be great if we could have a hook_ENTITY_TYPE_view_mode_alter(&$view_mode, \Drupal\Core\Entity\EntityInterface $entity) in order to avoid a test on the entity type when we have to use hook_entity_view_mode_alter and better target the entity types on which we want to act.
That would make the code more SOLID and readable.
I've set the version to 10.0.x-dev but since it would be a new feature, feel free to change it to 11.x-dev (even if it woudn't break anything on D10)
Steps to reproduce
NA
Proposed resolution
Add a new hook
Remaining tasks
Add documentation of new hook to entity.api.phpAdd test coverage/implementation of the hook - see\Drupal\Tests\node\Functional\NodeEntityViewModeAlterTest::testNodeViewModeChangefor an exampleUpdate EntityViewBuilder and any sub-classes that override::getBuildDefaultschange record
User interface changes
NA
API changes
New entity hook hook_ENTITY_TYPE_view_mode_alter that will "Change the view mode of a particular entity type that is being displayed."
Data model changes
NA
Release notes snippet
TBD
Issue fork drupal-3407912
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:
- 3407912-add-a-hookentitytypeviewmodealter
changes, plain diff MR !5775
Comments
Comment #2
larowlanThis could only go into 10.3
Are you sure this doesn't already exist?
Comment #3
macsim commentedI was surprised that it didn't exist but I can't find such a hook in the core ; there are a lot of
hook_ENTITY_TYPE_[something]in theentity.api.phpbut it seems that there's nohook_ENTITY_TYPE_view_mode_alter()declared at all.Edit :
Didn't checked on a Drupal 11 though.https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co... I can't find it in Drupal 11 as well (I only checked in
entity.api.php- that's where it should be)Comment #4
larowlanConfirming
\Drupal\Core\Entity\EntityViewBuilder::getBuildDefaultsdoesn't fire such a hook.I do note that you can override that method on a per entity-type basis via changing the view builder.
Comment #5
macsim commentedNot sure about how to achieve that... Is that the right way?
Comment #6
larowlanTo be clear, yeah its not ideal - you'd have to probably duplicate a lot of the parent code.
FWIW I'm plus one for this idea, its fairly self contained and consistent with other entity API hooks.
Added remaining tasks to issue summary
Comment #9
akhil babuAdded the hook and documented in entity.api.php
Comment #10
macsim commentedComment #11
macsim commentedAdded kernel tests
Do we have to also implements Functional tests for this?
Comment #12
macsim commentedMy bad I was only running tests on
EntityViewBuilderTest.phpin my local env before pushing.I guess I should have created new view_modes in my test instead of using "full" and "teaser" view_modes in order not to break any other test from another file.
Gonna work on this at the end of the week.
Comment #13
macsim commentedPipelines have been fixed by adding/using 2 new view modes
Back to "Kernel tests added / Do we also have to provide functional tests?"
Comment #14
smustgrave commentedNot sure I follow?
This will need a change record though,
Updated issue summary to the standard issue template and added missing sections.
Comment #15
arisenAdded the change record.
Comment #16
macsim commentedComment #17
macsim commentedDo we really need more tests than the kernel tests?
Comment #18
smustgrave commentedNot sure where the request for functional testing came from but believe the current coverage should be fine. Ran the test-only feature and get
Small comments on MR, but overall good and like 99% there.
Comment #20
prashant.cUpdated the docblock sentences in the hook documentation for clarity:
Additionally, incorporated review comments.
Thank you.
Comment #21
smustgrave commentedFeedback has been addressed.
Comment #23
catchThis looks good to me, just brings it into line with other entity alter hooks.
Committed/pushed to 11.x, thanks!