Set Entity View Mode

Created on 10 January 2024, 6 months ago
Updated 10 May 2024, about 2 months ago

Problem/Motivation

It would be good to change the Entity View Mode before it gets rendered.

Something similar can be already achieved with Form Mode using this ECA:
E: Prepare Content Entity View
C: Some conditions on the entity token
A: Entity: set form display

We already have the Event Prepare Content Entity View, so we just need the Entity: set view display that would set the view mode of the Entity.

Proposed resolution

There is already a hook named hook_entity_view_mode_alter() where we can add some actions and set the current views mode of the Entity.

So this would be something similar but using ECA's style :)

There are already some issues about this, but dedicated to get the current view mode to use on conditions, like ✨ Add optional View Mode condition to "View Content Entity" Event Fixed or generic rendering entities after display like #3281715: Provide a new module "eca_render" for various rendering capabilities → , but I think this last one can't define the view mode before it gets displayed.

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇵🇹Portugal jrochate

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

Merge Requests

Comments & Activities

  • Issue created by @jrochate
  • 🇵🇹Portugal jrochate
  • 🇵🇹Portugal jrochate
  • 🇩🇪Germany jurgenhaas Gottmadingen

    The hook hook_entity_view_mode_alter looks exactly right for this, and it should be hard to add an event for that together with a new action to set the view mode then. This could be an equivalent to what's already possible with forms, as @jrochate explained in the initial post.

    I guess we missed that hook when we implemented the original list of hooks and events, as we may not have seen this use case. But in this context, this makes perfect sense.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Just saw, that next to hook_entity_view_mode_alter there will also be hook_ENTITY_TYPE_view_mode_alter coming in Drupal 20.3, according to this change record: https://www.drupal.org/node/3410039 →

  • 🇵🇹Portugal jrochate

    Well, if we take a look at eca_content the Action "Entity: set form display" is not using a hook but a setFormDisplay() from Drupal\Core\Entity\ContentEntityForm.

    I've been looking at these interfaces available on core, but there is no equivalent setViewDisplay() or a similar Interface to deal.

    So I think the hook is the most approximate method.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Yes, the hook you posted is a good one and we will be implementing this. My comment in #5 was about an additional hook, which we should also implement, when we get around to work on this.

  • 🇵🇹Portugal jrochate

    This module Entity Role View Mode Switcher → is another nice example of what we could do in ECA:

    - the module sets some rules about roles and view modes
    - using the hook hook_entity_view_mode_alter() like we talked before, they get the node rendered according to the rules.

    The .module is basically just this:

    use Drupal\Core\Entity\EntityInterface;
    use Drupal\entity_role_view_mode_switcher\Util\ViewModeSwitcher;
    
    /**
     * Implements hook_entity_view_mode_alter().
     */
    function entity_role_view_mode_switcher_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
      $view_mode = ViewModeSwitcher::switchViewModes($entity, $view_mode, \Drupal::currentUser()->getRoles());
    }
    
    

    is we got this working on ECA, this module could be easily replaced and "extended", since we would get all the Conditions that ECA offers.

  • Merge request !409Issue #3413762: Set Entity View Mode → (Merged) created by jurgenhaas
  • Status changed to Needs review 4 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    This is now implemented in MR!409 for ECA 2:

    • There is an event Alter entity view mode which can be limited by entity type
    • That event automatically provides tokens, that contain the current view mode. Those are called [event:view_mode] and [entity_view_mode], both contain the same value, and they can be used e.g. in the string comparison condition to check for the current value.
    • There is also an action Entity: set view mode which allows setting another view mode. This action only works after the new event above. In another context, it would not execute.

    Please give it a try and let us know if it works for you.

  • First commit to issue fork.
  • Merge request !416Set Entity View Mode → (Open) created by lolgm
  • Pipeline finished with Success
    3 months ago
    Total: 367s
    #131857
  • 🇵🇹Portugal lolgm

    The MR!416 is the implementation of MR!409 for 1.1.x.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thank you, @lolgm, but before we start back porting, it would be better to review and test the proposed implementation. Otherwise, we're going to have extra work when things need to be adjusted and fixed.

  • 🇵🇹Portugal jrochate

    At the moment, the MR!416 is working fine on 1.1.x.

    I can't firmly test it on 2.0.x-dev for now.

  • Pipeline finished with Success
    3 months ago
    #138227
  • Pipeline finished with Skipped
    3 months ago
    #139517
  • Pipeline finished with Skipped
    2 months ago
    #157232
  • Status changed to Fixed 2 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    OK, I'll take #15 as an RTBC and merged this new feature into 2.0.x - users of the 1.1 version can grab the patch from !MR416 until we can all upgrade to the new release.

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

  • Pipeline finished with Success
    16 days ago
    Total: 350s
    #197241
Production build 0.69.0 2024