Add a hook_ENTITY_TYPE_view_mode_alter()

Created on 11 December 2023, about 1 year ago
Updated 9 February 2024, 11 months ago

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

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

โœจ Feature request
Status

Fixed

Version

11.0 ๐Ÿ”ฅ

Component
Entityย  โ†’

Last updated about 21 hours ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @catch
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland @berdir
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany @hchonov
Created by

๐Ÿ‡ซ๐Ÿ‡ทFrance MacSim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • 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 the entity.api.php but it seems that there's no hook_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.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Akhil Babu Chengannur

    Added th hook and documented in entity.api.php

  • Assigned to MacSim
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance MacSim
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ท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
  • ๐Ÿ‡ซ๐Ÿ‡ท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
  • ๐Ÿ‡ซ๐Ÿ‡ท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
  • ๐Ÿ‡บ๐Ÿ‡ธ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.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia arisen Goa

    Added the change record.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance MacSim
  • Status changed to Needs review about 1 year ago
  • ๐Ÿ‡ซ๐Ÿ‡ทFrance MacSim

    Do we really need more tests than the kernel tests?

  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธ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
  • ๐Ÿ‡ฎ๐Ÿ‡ณ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
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Feedback has been addressed.

    • catch โ†’ committed cbe1d286 on 11.x
      Issue #3407912 by Akhil Babu, MacSim, smustgrave, Prashant.c, larowlan,...
  • Status changed to Fixed 12 months ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Committed/pushed to 11.x, thanks!

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

Production build 0.71.5 2024