Disclaimer : this bug has not been filled against D8 because it seems that it has been fixed by moving the drupal_alter('entity_view_mode') call to Drupal\Core\Entity\EntityRenderController::viewMultiple()
It took me a lot of time to fill this issue because I was not sure it was not caused by a contrib module so I dug into the core then found the cause.
To illustrate the case, I made a tiny module (using Features for quickness)
Problem
The problem is that when you implements hook_entity_view_mode_alter to change the default view mode of a node to "custom_vm", this one's fields are prepared for view (hook_field_formatter_prepare_view) using the "full" view mode then show (hook_field_formatter_view) using the chosen view mode (custom_vm). Most of the time this will not be a problem but, with taxonomies for example this can lead to an error screen.
Steps to reproduce (very quick way)
- Launch a sandbox
- Go to node/1
Steps to reproduce (quick way) :
- Install drupal
- Download and enable my module
- Go to node/1
Steps to reproduce (long way) :
- Install drupal
- Create a vocabulary, add it a term
- Create a content type, add it a taxonomy reference field to your vocabulary
- In the content type display management, hide the taxonomy field from "full" view mode and show it on "teaser" one
- Create a node using your term
- Create a tiny module which implements hook_entity_view_mode_alter to change the view mode to "teaser" (see below)
- Go to node/1
Tiny example module which implements hook_entity_view_mode_alter
<?php
/**
* Implements hook_entity_view_mode_alter().
*/
function MODULE_entity_view_mode_alter(&$view_mode, $context) {
$view_mode = 'teaser';
}
?>
Attachment | Size | Status | Test result | Operations |
---|---|---|---|---|
hook_entity_view_mode_alter_error.png | 24.15 KB | Ignored | None | None |