- Issue created by @MacSim
- ๐ฆ๐บAustralia larowlan ๐ฆ๐บ๐.au GMT+10
This could only go into 10.3
Are you sure this doesn't already exist?
- ๐ซ๐ทFrance MacSim
I 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.php
but it seems that there's nohook_ENTITY_TYPE_view_mode_alter()
declared at all - ๐ฆ๐บAustralia larowlan ๐ฆ๐บ๐.au GMT+10
Confirming
\Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults
doesn'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.
- ๐ซ๐ทFrance MacSim
Not sure about how to achieve that... Is that the right way?
mymodule.module
/** * Implements hook_entity_type_alter(). */ function mymodule_entity_type_alter(array &$entity_types): void { if (isset($entity_types['node'])) { $entity_types['node']->setViewBuilderClass(NodeViewBuilder::class); } }
class NodeViewBuilder extends EntityViewBuilder { protected function getBuildDefaults(EntityInterface $entity, $view_mode) { // Can I call the parent protected method in order to alter its result? // or do I have to copy/paste/alter/maintain that parent method in here? } }
Is that right?
- ๐ฆ๐บAustralia larowlan ๐ฆ๐บ๐.au GMT+10
To 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
- ๐ฎ๐ณIndia Akhil Babu Chengannur
Akhil Babu โ made their first commit to this issueโs fork.
- Merge request !5775Issues/3407912: Add hook_ENTITY_TYPE_view_mode_alter(). โ (Open) created by Akhil Babu
- ๐ฎ๐ณIndia Akhil Babu Chengannur
Added th hook and documented in entity.api.php
- Assigned to MacSim
- Issue was unassigned.
- Status changed to Needs review
about 1 year ago 11:57pm 13 December 2023 - ๐ซ๐ทFrance MacSim
Added kernel tests
Do we have to also implements Functional tests for this? - Assigned to MacSim
- Status changed to Needs work
about 1 year ago 12:19am 14 December 2023 - ๐ซ๐ทFrance MacSim
My bad I was only running tests on
EntityViewBuilderTest.php
in 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. - Issue was unassigned.
- Status changed to Needs review
about 1 year ago 1:24pm 17 December 2023 - ๐ซ๐ทFrance MacSim
Pipelines have been fixed by adding/using 2 new view modes
Back to "Kernel tests added / Do we have to also provide functional tests?"
- Status changed to Needs work
about 1 year ago 5:38pm 17 December 2023 - ๐บ๐ธUnited States smustgrave
"Kernel tests added / Do we also have to provide functional tests?"
Not sure I follow?
This will need a change record though,
Updated issue summary to the standard issue template and added missing sections.
- Status changed to Needs review
about 1 year ago 11:11pm 27 December 2023 - ๐ซ๐ทFrance MacSim
Do we really need more tests than the kernel tests?
- Status changed to Needs work
about 1 year ago 5:12pm 28 December 2023 - ๐บ๐ธUnited States smustgrave
Not sure where the request for functional testing came from but believe the current coverage should be fine. Ran the test-only feature and get
1) Drupal\KernelTests\Core\Entity\EntityViewBuilderTest::testHookEntityTypeViewModeAlter Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'entity_test.vm_alter_full' +'entity_test.vm_alter_test' /builds/issue/drupal-3407912/vendor/phpunit/phpunit/src/Framework/Constraint/Equality/IsEqual.php:94 /builds/issue/drupal-3407912/core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php:336 /builds/issue/drupal-3407912/vendor/phpunit/phpunit/src/Framework/TestResult.php:728 FAILURES!
Small comments on MR, but overall good and like 99% there.
- ๐ฎ๐ณIndia prashant.c Dharamshala
Prashant.c โ made their first commit to this issueโs fork.
- Status changed to Needs review
about 1 year ago 12:05pm 29 December 2023 - ๐ฎ๐ณIndia prashant.c Dharamshala
Updated the docblock sentences in the hook documentation for clarity:
/** - * Change the view mode of a particular entity type that is being displayed. + * Change the view mode of a specific entity type currently being displayed. * * @param string $view_mode - * The view_mode that is to be used to display the entity. + * The view_mode currently displaying the entity. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity that is being viewed. * * @ingroup entity_crud */
Additionally, incorporated review comments.
Thank you. - Status changed to RTBC
about 1 year ago 5:43pm 29 December 2023 - Status changed to Fixed
12 months ago 9:41am 26 January 2024 Automatically closed - issue fixed for 2 weeks with no activity.