Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 291082

hook_field_formatter_prepare_view do not make use of hook_entity_view_mode_alter causing major errors

$
0
0

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.

hook_entity_view_mode_alter_error.png

Steps to reproduce (very quick way)

  1. Launch a sandbox
  2. Go to node/1

Steps to reproduce (quick way) :

  1. Install drupal
  2. Download and enable my module
  3. Go to node/1

Steps to reproduce (long way) :

  1. Install drupal
  2. Create a vocabulary, add it a term
  3. Create a content type, add it a taxonomy reference field to your vocabulary
  4. In the content type display management, hide the taxonomy field from "full" view mode and show it on "teaser" one
  5. Create a node using your term
  6. Create a tiny module which implements hook_entity_view_mode_alter to change the view mode to "teaser" (see below)
  7. 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';
}
?>
AttachmentSizeStatusTest resultOperations
hook_entity_view_mode_alter_error.png24.15 KBIgnoredNoneNone

Viewing all articles
Browse latest Browse all 291082

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>