Labs

By John Ennew | 23rd March 2012

Display Suite - Getting even more control of your fields

When you want even more control over an outputted field, you can still put a theme function in template.php file. With the devel module enabled, set the display mode for a field to expert then add the following to template.php, clear your cache and then load a page with the field on it.

/**
 * Implements hook_field__expert__FIELD_ID().
 */
function mytheme_field__expert__myfield(&$variables) {
  // Inspect the contents of $variables.
  krumo($variables);
 
  // Pass back to the display suite theme function for final theming.
  return theme_ds_field_expert($variables);
}

In this manner you can inspect the variables in $variables, make changes to them then let display suite format the output as before.

A good item to inspect is $variables['element']['#view_mode'] which will give you the current view mode the field is being rendered in. If you have a view list of teasers for instance, this will say teaser, allowing you to then modify the field just on that one display, leaving the output on the main node page alone. Below we see an example of just outputting one image of an unlimited image field in teaser mode.

/**
 * Implements hook_field__expert__FIELD_ID().
 */
function mytheme_field__expert__field_images(&$variables) {
    if ($variables['element']['#view_mode'] == 'teaser') {
      // In teaser mode, only output the first image.
      $item = $variables['items'][0];
      $variables['items'] = array(0 => $item);
    }
  return theme_ds_field_expert($variables);
}

Comments

Alex Burrows
Alex Burrows

I'm using it for a big UK project soon, so will write something up for Tim on my thoughts of it once I've used it. But it does look very encouraging!

Mark
Mark

Display Suite is amazing, i cant imagine not using but looking forward to try out Panelizer to see how it matches up

Alex Burrows
Alex Burrows

Panelizer new features are going to take on Display Suite head on, Earl showed the new features and it does look impressive. Never really delved into Display Suite tbh.

Lars Olesen
Lars Olesen

@Alex Burrows: Have been looking for a screencast showing what Panelizer can do, but cannot find one. Could you point me in the right direction?

Alex Burrows
Alex Burrows

Add new comment