You are here

function ds_build_fields_and_regions in Display Suite 6

Same name and namespace in other branches
  1. 6.3 includes/ds.api.inc \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.:

File

./ds.module, line 180

Code

function ds_build_fields_and_regions(&$object, $module) {

  // API info for this module and type.
  $api_info = ds_api_info($module, $object->type);

  // 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());
  $object->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;
  $regions = array();
  $ds_fields = array();
  $ds_groups = array();
  $ds_preprocess_fields = array();
  $display_settings = variable_get($module . '_display_settings_' . $object->type, array());

  // Get all fields and settings for this build mode.
  $fields = ds_get_fields($module, $object->type, $object->build_mode, $api_info['extra']);
  $field_settings = ds_default_value($display_settings, $object->build_mode, 'fields');

  // Get the object type for field rendering and tokenization.
  $object_type = $api_info['object'];
  if (!empty($fields)) {
    foreach ($fields as $key => $field) {

      // Exclude depends on the region.
      $region = isset($field_settings[$key]['region']) ? $field_settings[$key]['region'] : DS_DEFAULT_REGION;
      if ($region != 'disabled') {

        // Default class and extra class from the UI.
        $class = isset($field['properties']['css-class']) ? $field['properties']['css-class'] : 'field-' . strtr($key, '_', '-');
        if (isset($field_settings[$key]['css-class']) && !empty($field_settings[$key]['css-class'])) {
          $class .= ' ' . $field_settings[$key]['css-class'];
        }
        if (isset($field['type']) && $field['type'] == DS_FIELD_TYPE_GROUP) {
          $class .= ' field-group';
        }

        // Build field key.
        $ds_fields[$key]['key'] = $key;
        $ds_fields[$key]['region'] = $region;
        $ds_fields[$key]['title'] = $field['title'];
        $ds_fields[$key]['class'] = $class;
        $ds_fields[$key]['labelformat'] = isset($field_settings[$key]['labelformat']) ? $field_settings[$key]['labelformat'] : DS_DEFAULT_LABEL_FORMAT;
        $ds_fields[$key]['type'] = isset($field['display_settings']) ? 'other' : 'ds';

        // Check for weight in region and parent (if any). If a parent key is found, we'll unset the original
        // field from the region it might be set in and we'll add that field to the group array.
        $weight = isset($field_settings[$key]['weight']) ? $field_settings[$key]['weight'] : DS_DEFAULT_WEIGHT;
        $parent = isset($field_settings[$key]['parent']) ? $field_settings[$key]['parent'] : '';
        $ds_fields[$key]['weight'] = $weight;
        $regions[$region][$key] = $weight;
        if (!empty($parent)) {

          // Remove the field from the $regions array.
          unset($regions[$region][$key]);

          // Get the form of the group and create a theme key.
          $format = isset($field_settings[$parent]['format']) ? $field_settings[$parent]['format'] : 'ds_group_fieldset';
          $ds_fields[$parent]['theme'] = $format;
          $ds_groups[$parent][$key] = $weight;
        }

        // If the field has the storage key, it means the theming is
        // done in that module and we can skip the rest of the loop.
        if (isset($field['storage'])) {
          continue;
        }

        // Change the title if this is configured and label is not hidden.
        if (isset($field_settings[$key]['label_value']) && $ds_fields[$key]['labelformat'] != DS_DEFAULT_LABEL_FORMAT) {
          $ds_fields[$key]['title'] = t(check_plain($field_settings[$key]['label_value']));
        }

        // Add extra properties.
        $field['key'] = $key;
        $field['module'] = $module;
        $field['object'] = $object;
        $field['object_type'] = $object_type;

        // Create a $key_rendered variable.
        $key_rendered = $key . '_rendered';

        // Theming can either be done in preprocess, with a custom function or an
        // existing formatter theming function. Always pass the $field as parameter.
        switch ($field['type']) {
          case DS_FIELD_TYPE_PREPROCESS:
          case DS_FIELD_TYPE_IGNORE:
            if (isset($field['properties']['key']) && !empty($field['properties']['key'])) {
              $ds_preprocess_fields[$key] = array(
                'type' => $field['type'],
                'key' => $field['properties']['key'],
              );
            }
            else {
              $ds_preprocess_fields[$key] = $field['type'];
            }
            break;
          case DS_FIELD_TYPE_CODE:
            $format = isset($field_settings[$key]['format']) ? $field_settings[$key]['format'] : 'ds_eval_code';
            $field['formatter'] = $format;
            $object->{$key_rendered} = theme($format, $field);
            break;
          case DS_FIELD_TYPE_BLOCK:
            $object->{$key_rendered} = theme('ds_eval_block', $field);
            break;
          case DS_FIELD_TYPE_FUNCTION:
            if (isset($field['file'])) {
              include_once $field['file'];
            }
            $function = isset($field_settings[$key]['format']) ? $field_settings[$key]['format'] : key($field['properties']['formatters']);
            $object->{$key_rendered} = $function($field);
            break;
          case DS_FIELD_TYPE_THEME:
            $format = isset($field_settings[$key]['format']) ? $field_settings[$key]['format'] : key($field['properties']['formatters']);
            $field['formatter'] = $format;
            $object->{$key_rendered} = theme($format, $field);
            break;
        }
      }
    }
  }

  // Add fields, groups and regions to the object.
  // Also reset render_by_ds property if needed.
  $object->ds_fields = $ds_fields;
  $object->ds_groups = $ds_groups;
  $object->regions = $regions;
  $object->preprocess_fields = $ds_preprocess_fields;
  if (empty($regions)) {
    $object->render_by_ds = FALSE;
  }
}