- Issue created by @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 behook_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 hookhook_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.
- Status changed to Needs review
11 months ago 1:16pm 1 March 2024 - 🇩🇪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.
- There is an event
- First commit to issue fork.
- 🇩🇪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.
-
jurgenhaas →
committed 6e2eca87 on 2.0.x
Issue #3413762 by jurgenhaas, jrochate: Set Entity View Mode
-
jurgenhaas →
committed 6e2eca87 on 2.0.x
- Status changed to Fixed
9 months ago 8:20am 26 April 2024 - 🇩🇪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.