Skip to main content

Changing Display Suite fields in update hooks

An article from ComputerMinds - Building with Drupal in the UK since 2005
10th Feb 2011

James Williams

Senior Developer
James Williams
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

We've been using Display Suite with Node displays on one of our latest sites. It was an experiment, and possibly not something we'll do again - DS offers some great functionality with build modes and ways to re-arrange fields and all sorts on nodes, but since we end up theming most nodes quite a lot anyway, it's not worth the extra hassle that Display Suite can add when you want to change the theming on DS fields, layouts, change the fields' settings etc...

We use update hooks to change most things on sites for clients, since this allows us to ensure sites will always have the same state on any machine without having to transfer database dumps around all the time between multiple developers & development sites. DS doesn't take nicely to this, because it doesn't check it's own default settings in code, but instead looks at the (CCK) field display settings in the database! Which is odd, since it's UI does check for exported default settings. (You can export your site's field settings at /admin/build/ds/tools/export - follow the instructions to implement hook_ds_default_settings()

This can be frustrating when a field's display settings need changing, because while changing the exported default DS field settings in code is simple, it's not so simple to update CCK's field display settings. So here's how to do it in an update hook (this updates fields in the user_profile node type). This example only updates the build modes set in $build_modes:


/**
 * Implements hook_update_N().
 */
function MYMODULE_update_N() {
  $ret = array();

  // Set the build modes you want to update here:
  $build_modes = array(
    'header_extended' => 'Header (extended)',
    'consultant_card' => 'Consultant card',
    'client_card' => 'Client card',
    'contact_card' => 'Contact card',
  );

  // Get hold of the CCK field settings for our node type:
  module_load_include('inc', 'content', 'includes/content.crud');
  $type_name = 'user_profile'; // Change 'user_profile' to be whatever your node type is.
  $type = content_types($type_name);

  // Get hold of the exported DS settings:
  $settings = module_invoke_all('ds_default_settings');
  $settings = $settings['nd'][$type_name]['header_extended']['fields'];

  // Shove the DS settings for each field into the display_settings array for each CCK field:
  foreach ($type['fields'] as $name => $field) {
    foreach ($build_modes as $mode_id => $mode_title) {
      $field['display_settings'][$mode_id] = $field['display_settings']['header'];
      $field['display_settings'][$mode_id]['format'] = $settings[$name]['format'];
      $field['display_settings'][$mode_id]['label']['format'] = $settings[$name]['labelformat'];
      $field['display_settings'][$mode_id]['region'] = $settings[$name]['region'];
      $field['display_settings'][$mode_id]['weight'] = $settings[$name]['weight'];
      $field['display_settings'][$mode_id]['css-class'] = $settings[$name]['css-class'];
      $field['display_settings'][$mode_id]['label_value'] = $settings[$name]['label_value'];
    }
    // Finally, update $field.
    content_field_instance_update($field);
  }
  foreach ($build_modes as $mode) {
    $ret[] = array('success' => TRUE, 'query' => "Configured the {$mode} build mode.");
  }
  return $ret;
}

(Replace MYMODULE with your module's name, and change the N in the function name with the update hook number - see the documentation for hook_update_N() for the naming scheme.)

Display Suite's API documentation is fairly good, so do read it to learn more about exporting fields/settings, or drop a comment below with your questions/needs!

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.