Problem/Motivation
As it states in the issue title, the $context array is always empty because of:
protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
// Allow modules to change the view mode.
$context = [];
$this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);
See: https://git.drupalcode.org/project/drupal/-/blob/9.2.x/core/lib/Drupal/C...
It wouldn't be that bad, but documentation of hook_entity_view_mode_alter()
says the langcode
should be available there:
/**
* Change the view mode of an entity that is being displayed.
*
* @param string $view_mode
* The view_mode that is to be used to display the entity.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity that is being viewed.
* @param array $context
* Array with additional context information, currently only contains the
* langcode the entity is viewed in.
*
* @ingroup entity_crud
*/
function hook_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
// For nodes, change the view mode when it is teaser.
if ($entity->getEntityTypeId() == 'node' && $view_mode == 'teaser') {
$view_mode = 'my_custom_view_mode';
}
}
Looking at the commit history, the $langcode was removed ~5 years ago in #2073217: Remove the $langcode parameter from the entity view/render system. Since that time, $context
remains empty.
Proposed resolution
I see two options here, but I'm not sure which one is preferred:
1) Update the documentation and mention that $context
is empty will be removed later (kind of deprecation);
2) Remove $context
variable from hook_entity_view_mode_alter()
.
Issue fork drupal-3193131
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:
- 9.2.x compare
- 3193131-entity-view-mode-alter-context changes, plain diff MR !257
Comments
Comment #3
MatroskeenOpened a merge request following 2nd option 👻
Looking forward to other opinions.
Comment #4
daffie CreditAttribution: daffie commentedYou cannot just remove a parameter from a method. It needs to be deprecated and later in a new major version can it be removed.
Or can you remove the parameter and when somebody is setting the parameter do not have an error? If that is the case then it is not a problem.
Do we need to update the node_test module as it is implementing the hook?
Comment #5
MatroskeenUpdated
node_test_entity_view_mode_alter
in node_test module.After applying these changes, for the existing implementations, the $context value will be NULL instead of empty array.
It'll be an error, but I don't see why someone would set the value into
$context
array. This is not expected here. The$context
is responsible for providing some extra information and the$view_mode
is expected to be changed.Comment #6
daffie CreditAttribution: daffie commentedThe parameter
$context
has always a value of an empty array.I did a code search on xnddx for implementation of the hook
hook_entity_view_mode_alter()
and all implementation ignore the parameter. I also do not see how we can deprecate the parameter. Therefore lets just remove the parameter. The only downside I can see is that implementation of the hook now have the parameter$context
with the value NULL instead of an empty array.What I miss is a CR. After that is done is the issue for me RTBC and lets see what the committers think about it.
Comment #7
MatroskeenAdded a draft change record: https://www.drupal.org/node/3193299.
Comment #8
daffie CreditAttribution: daffie commentedThe parameter
$context
has always a value of an empty array.I did a code search on xnddx for implementation of the hook
hook_entity_view_mode_alter()
and all implementation ignore the parameter. I also do not see how we can deprecate the parameter. Therefore lets just remove the parameter. The only downside I can see is that implementation of the hook now have the parameter$context
with the value NULL instead of an empty array.Therefore the best solution for me is just to remove the parameter. The issue is for me RTBC and lets see what the committers think about it.
Comment #9
catchOne comment on the PR - I think we need to keep passing the empty array in 9.x with a follow-up to stop doing that in 10.x
Comment #10
daffie CreditAttribution: daffie commentedWe are already doing that in 9.x. Therefore moving this to 10.0.
Comment #11
MatroskeenAs I understand, we can remove it from the documentation and hook signature, so new implementations won't have it.
However, we'll still pass it along so old implementations will have it. MR was updated to reflect these changes.
The actual removal will be done in a new task that will be created for 10.x version. I assume a change record should be also published when the follow-up task will be committed.
I'm wondering if we need to add some information to the
hook_entity_view_mode_alter()
documentation. I think it won't help anyway.Comment #12
catch#11 is right, and the updated MR looks good to me, as does the change record.
Yeah I wondered as well, but don't see it helping anyone - better to have it reflect reality. It might have been helpful when the original change was made years back.
Comment #13
catchOpened #3194165: Stop passing empty $context array to hook_entity_view_mode_alter() for the 10.x-only bit.
Comment #15
catchCommitted f58b01f and pushed to 9.2.x. Thanks!