You are here

function dsDisplay::prepare in Display Suite 6.3

Build field and region information

Parameters

stdClass $object : The object for which this display is being built. We'd prefer to just attach it directly, but this causes circular reference problems in PHP 5.2

File

includes/dsDisplay.php, line 166
Class definition for a Display Suite Display object

Class

dsDisplay
The Display Suite Display object

Code

function prepare(&$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($this->settings['fields'])) {

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

    // 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 ($this->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', $this->api_info['object']);
        $this
          ->addField($field_key, $field);
      }
    }

    // Build the individual fields using settings created previously
    $temp_regions = array();
    foreach ($this->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') {
        $this->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();
        $this
          ->nestFields($region, $nested_fields);
        $this->regions[$key] = $nested_fields;
      }

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

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