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 $contextis empty will be removed later (kind of deprecation);
2) Remove $context variable from hook_entity_view_mode_alter().

Issue fork drupal-3193131

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Matroskeen created an issue. See original summary.

Matroskeen’s picture

Issue summary: View changes
Status: Active » Needs review
Related issues: +#2073217: Remove the $langcode parameter from the entity view/render system

Opened a merge request following 2nd option 👻
Looking forward to other opinions.

daffie’s picture

You 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?

Matroskeen’s picture

Updated 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.

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.

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.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

The 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.

Matroskeen’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

Added a draft change record: https://www.drupal.org/node/3193299.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The 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.

catch’s picture

Status: Reviewed & tested by the community » Needs work

One 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

daffie’s picture

Version: 9.2.x-dev » 10.0.x-dev
Issue tags: +Drupal 10

I think we need to keep passing the empty array in 9.x with a follow-up to stop doing that in 10.x

We are already doing that in 9.x. Therefore moving this to 10.0.

Matroskeen’s picture

Version: 10.0.x-dev » 9.2.x-dev
Status: Needs work » Needs review
Issue tags: -Drupal 10

As 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.

catch’s picture

Status: Needs review » Reviewed & tested by the community

#11 is right, and the updated MR looks good to me, as does the change record.

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.

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.

catch’s picture

  • catch committed f58b01f on 9.2.x
    Issue #3193131 by Matroskeen, daffie, catch: $context in...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed f58b01f and pushed to 9.2.x. Thanks!

Status: Fixed » Closed (fixed)

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