2

I would like to create a view that displays a number of nodes, each using its own chosen view mode.

I've installed the Display Suite module and created a number of view modes. Using the Views module, I've created a view that displays a number of entities/nodes and I can set a view mode for each row, or a default view mode for all rows.

However, I would like the view to somehow use the view mode that has been selected when creating the content itself (under the display settings tab).

So far I have no idea how to achieve this. Could anyone give me some pointers?

1 Answer 1

0

First you need to save the view mode for each entity (e.g. in a field).

Implementing hook_entity_view_mode_alter you can override the view mode:

function mymodule_entity_view_mode_alter(&$view_mode, $context) {
  if ($context['entity_type'] == 'node') {
    $entity = $context['entity']
    if (get the view mode of $entity) {
      $view_mode = 'custom_view_mode';
    }
  }
}

Remember to do a check to run this in your view only, otherwise this will affect the node everywhere.

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.