3

Basically I want to let the content creator choose an arbitrary view mode. For example, be able to choose a gallery that is a FlexSlider slideshow, a Masonry jQuery plugin type image gallery, or an image gallery that displays metadata below each image.

On the entity there would be a field called field_view_mode which is an options list. The options are flex slider, masonry, and full images with text. Depending on the option they get the images and other fields formatted differently in different templates.

I'm thinking about defining 5 different arbitrary view modes. One more view mode would be custom_view_mode.

function hook_entity_view_mode_alter(&$view_mode, $context) {
  // For nodes, change the view mode when it is teaser.
  if ($context['entity_type'] == 'node' && $view_mode == 'custom_view_mode') {
    $view_mode = 'use entity metadata wrapper to pull field_view_mode value here';
  }
}

I don't want to use the Template Field module because it seems like overkill. I'm not sure if the Display Suite module does this but that seems like overkill too. I just want different view modes to be available. Has anyone done this before?

1 Answer 1

2

I know it's been a while since you asked...

As far as I can say, if you want use your own module, and not a contributed one, you have to define the view modes using hook_entity_info_alter, for example:

function mycustommodule_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['mycustommode'] = array(
  'label' => t('My custom view mode'),
  'custom settings' => FALSE, //Or true if you want to allow a certain role to modify it 
  );
}

After that, you can use the hook_entity_view_mode_alter from your custom module (not working if you are trying to use in from your template.php in your theme). Hope that helps to anyone coming here from search as I did.

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.