You are here

function ds_build_fields_and_regions in Display Suite 6.3

Same name and namespace in other branches
  1. 6 ds.module \ds_build_fields_and_regions()
  2. 6.2 ds.module \ds_build_fields_and_regions()

Get fields and regions for an object.

Parameters

stdClass $object : The object to manipulate.

string $module : The module that is requesting.

1 call to ds_build_fields_and_regions()
ds_make in includes/ds.api.inc
API function to build and render a build mode for a given object

File

includes/ds.api.inc, line 86
API functions for building and rendering displays

Code

function ds_build_fields_and_regions(&$object, $module) {

  // Initialise the display
  $display = ds_create_display();
  $display
    ->initialise($object);

  // See if rendering is needed later on.
  // There are two ways of excluding: global exclude on object type
  // or per build mode per object type.
  // We don't return here, because we want to add all fields on the object
  // so themers can use it in their template.
  $exclude_build_modes = variable_get($module . '_buildmodes_exclude', array());
  $render_by_ds = isset($exclude_build_modes[$object->type][$object->build_mode]) && $exclude_build_modes[$object->type][$object->build_mode] == TRUE || variable_get($module . '_type_' . $object->type, FALSE) == TRUE ? FALSE : TRUE;
  if (!empty($display->settings['fields'])) {

    // Get all fields and settings for this build mode (this doesnt load CCK fields, only groups)
    $available_fields = ds_get_fields($module, $object->type, $object->build_mode, $api_info['extra']);

    // The node Body can sometimes be populated with other content when empty
    // @todo - we do this here so the fields populate correctly, but find
    // somewhere logical for this
    if (isset($object->has_empty_body) && $object->has_empty_body == 1) {
      $object->body = '';
    }

    // Iterate over fields listed on the display
    foreach ($display->settings['fields'] as $field_key => $field_settings) {

      // Dont render fields which are set to disabled
      $region = isset($field_settings['region']) ? $field_settings['region'] : DS_DEFAULT_REGION;
      if ($region != DS_DISABLED_REGION) {

        // Create a dummy region in $temp_regions for later nesting
        if (!isset($temp_regions[$region])) {
          $temp_regions[$region] = array();
        }

        // @todo
        // Settings per field should be retrieved from a single cached function
        // call to ds_get_settings (or similar)
        if (isset($available_fields[$field_key])) {
          $field_defaults = $available_fields[$field_key];
        }
        else {
          $field_defaults = array();
        }
        $settings = array_merge($field_settings, $field_defaults);
        $field = ds_create_field($field_settings['type']);
        $field
          ->initialise($settings);
        $field
          ->setting('region', $region);
        $field
          ->setting('module', $module);
        $field
          ->setting('object', $object);
        $field
          ->setting('object_type', $display->api_info['object']);
        $display
          ->addField($field_key, $field);
      }
    }

    // Build the individual fields using settings created previously
    $temp_regions = array();
    foreach ($display->fields as $key => $field) {

      // Build the field
      $field
        ->build();

      // Create a temp reference only if it doesnt already exists
      // This ensures that the weight and region always comes from the first
      // fieldgroup positioned.
      $region_key = $field['region'];
      $parent = $field['parent'];
      $temp_regions[$region_key][$key] = array(
        '#weight' => $object->ds_fields[$key]['weight'],
        '#parent' => $parent,
      );
      if ($parent != '#root') {
        $display->fields[$parent]['fields'][] = $key;
      }
    }

    // Nest and order fields in regions
    if (!empty($temp_regions)) {

      // Nest groups
      foreach ($temp_regions as $key => $region) {
        $nested_fields = array();
        $display
          ->nestFields($region, $nested_fields);
        $display->regions[$key] = $nested_fields;
      }

      // Sort groups
      foreach ($object->regions as $key => $region) {
        $display
          ->orderFields($object->regions[$key]);
      }
    }
  }

  // Reset render_by_ds property if needed.
  if (empty($object->regions)) {
    $object->render_by_ds = FALSE;
  }
  $object->display = $display;
}