2

I have a paragraph (I'm using the Paragraphs module) that contains an entity reference field that contains references to other pages.

The display settings are set to use a particular view mode, e.g. teaser. This renders an teaser image, title and link to the page.

I would like the editor to be able to set the view mode on a per-paragraph basis. I have created a "view mode" field in the paragraph with two options: teaser, and teaser-text-only.

How can I use the view mode set in the "parent" entity (the paragraph) to add a template suggestion when rendering the "child" entities (the pages)?

3 Answers 3

8

At this point it should be no problem to change the view mode in the render element of the referenced node. The node has yet to be built and will, hopefully, use the modified view mode.

You can try something like this in a paragraph preprocess hook:

function mytheme_preprocess_paragraph(&$variables) {

  if ($variables['paragraph']->hasField('field_view_mode')) {
    $view_mode = $variables['paragraph']->field_view_mode->value;
    if (isset($variables['content']['field_ref_node']['0']['#view_mode'])) {
      $variables['content']['field_ref_node']['0']['#view_mode'] = $view_mode;
      unset($variables['content']['field_ref_node']['0']['#cache']['keys']);
    }
  }

}

Edit:

To prevent that the node gets cached on it's own in the wrong view mode remove the cache key.

6
  • 4k4 again! Shouldn't I be setting new cache keys instead of just deleting them?
    – Tim
    Commented Apr 10, 2017 at 5:29
  • That depends on how often you use the referenced entity in different paragraphs and if it is worth caching it on it's own. One way or the other, you have to take care of that cache keys I pointed out.
    – 4uk4
    Commented Apr 10, 2017 at 6:16
  • Thanks! I had to implement this in a module instead of the theme using hook_node_view_alter() to work. In the theme neither hook_node_view() nor hook_preprocess_node() worked; all adjustments to the media image (entity reference) were ignored / reset. See also drupal.stackexchange.com/questions/207092/…
    – sun
    Commented Mar 21, 2018 at 10:17
  • @4k4 are you populating the field_view_mode field by running a query to pull the current view modes for Paragraph? Also, what field type if the field_view_mode? Something along the lines of $view_modes = Drupal::entityQuery('entity_view_mode')->condition('targetEntityType', 'paragraph')->execute(); Commented Dec 3, 2018 at 21:48
  • 1
    @Tim 4k4 solution looks good then for what you need. An alternative to hard coding would be to use the view_mode_selector module as it would pull the view modes for you. Commented Dec 5, 2018 at 18:14
0

There is a module view_mode_selector that should allow for this now. There is currently a dev releases for D8.

A small module which allows you to change the view mode of an entity.

This module provides a new field type "View mode selector", so just go to "Manage fields" and create a field of that type and choose between a "select list" and a "radio buttons" widget. When you choose "radio buttons" it is possible to upload an icon which will be displayed instead of the view mode name.

In order to display your referenced entities with the new "View mode selector" view mode don't forget to change the view mode on "Manage display" which is used for displaying them.

If Display Suite is enabled, it can also display a live preview of the configured view mode layout. Here is an example:

1
  • I tested this module, this field only show display modes of current entity to which this field is attached for example paragraph, not the referenced entity (like nodes).
    – Sharique
    Commented Sep 9, 2022 at 10:36
0

Bit late to the party here but here's how I just did it using Drupal 7.

I have a paragraph type called Related Content (related_content) with 2 fields: An entity reference field and a Select (text) field called field_related_content_view_mode

I have set the default display mode of the referenced entities / field_related content in the field display of the related content paragraph and use hook_field_display_alter() to alter the display mode to the one set by field_related_content_view_mode:

/**
 * Implements hook_field_display_alter().
 */
function mymodule_field_display_alter(&$display, $context) {
  if ($context['entity_type'] == 'paragraphs_item') {
    if ($context['field']['field_name'] == 'field_related_content') {
      $entity = $context['entity'];
      if (isset($entity->field_related_content_view_mode) && !empty($entity->field_related_content_view_mode)) {
        $view_mode = $entity->field_related_content_view_mode[LANGUAGE_NONE][0]['value'];
        $display['settings']['view_mode'] = $view_mode;
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.