How to programmatically change the order of display suite fields when rendering

Sometimes Display Suite will render fields out of order. Use hook_ds_pre_render_alter() to change the order. In the following example, I update the order of fields in a particular content type. For some reason, Display Suite was rendering them out of order.

<?php
/**
 * Implements hook_ds_pre_render_alter().
 * 
 * Orders the display of Display Suite fields for the Example Content Type. Without this
 * implementation, some fields may render out of order.
 *
 * @see https://www.drupal.org/node/2035531#comment-9563515
 */
function example_ds_pre_render_alter(&$layout_render_array, &$context, &$variables) {
  if (!empty($context['bundle']) && $context['bundle'] === 'example_content_type') {
    foreach($layout_render_array as $region => &$fields) {
      foreach ($fields as $field_key => &$field) {
        $field['#weight'] = $field_key;
      }
    }
  }
}
?>

Tags

Internal References

External References

Article Type

General