You are here

function ds_build_field in Display Suite 6.2

Build an individual field value.

Prepares a field for ds to pass to ds_render_content. This does not resolve parent relationships.

Parameters

string $field_key: The field to build

array $field_settings: An array of field settings

Return value

array A field settings array ready to pass to ds_render_content

1 call to ds_build_field()
ds_build_fields_and_regions in ./ds.module
Get fields and regions for an object.

File

./ds.module, line 440
Core functions for the Display Suite module.

Code

function ds_build_field($field_key, $field_settings) {

  // Default class and extra class from the UI.
  $classes = array();
  $classes[] = 'field-' . strtr($field_key, '_', '-');
  if (isset($field_settings['properties']['css-class']) && !empty($field_settings['properties']['css-class'])) {
    $classes[] = $field_settings['properties']['css-class'];
    unset($field_settings['properties']['css-class']);
  }
  if (isset($field_settings['css-class']) && !empty($field_settings['css-class'])) {
    $classes[] = $field_settings['css-class'];
    unset($field_settings['css-class']);
  }

  // Field defaults - all fields get these.
  // @todo Abstract field types into config functions which returns defaults for
  // that type.
  $field_defaults = array(
    'labelformat' => DS_DEFAULT_LABEL_FORMAT,
    'label' => '',
    'theme' => DS_DEFAULT_THEME_FIELD,
    'weight' => DS_DEFAULT_WEIGHT,
    'content' => NULL,
  );

  // Merge defaults and settings to produce the field array.
  $field = array_merge($field_defaults, $field_settings);
  $field['key'] = $field_key;
  $field['type'] = empty($field_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.
  if (isset($field_settings['weight'])) {
    $field['weight'] = $field_settings['weight'];
  }
  $field['parent'] = isset($field_settings['parent']) ? $field_settings['parent'] : NULL;

  // Process groups (fieldsets).
  if ($field['field_type'] == DS_FIELD_TYPE_GROUP || $field['field_type'] == DS_FIELD_TYPE_MULTIGROUP) {
    if (isset($field_settings['format'])) {
      $field['theme'] = $field_settings['format'];
    }
    else {
      $field['theme'] = DS_DEFAULT_THEME_FIELDSET;
    }

    // Additional formatting settings for fieldsets.
    if ($field_settings['field_type'] == DS_FIELD_TYPE_GROUP) {
      $classes[] = 'field-group';
    }

    // Additional formatting settings for CCK multigroups.
    if ($field_settings['field_type'] == DS_FIELD_TYPE_MULTIGROUP) {
      $field['subgroup_theme'] = isset($field_settings['subgroup_format']) ? $field_settings['subgroup_format'] : DS_DEFAULT_THEME_FIELDSET;
      $classes[] = 'field-multigroup';
    }
  }
  $field['class'] = implode(' ', $classes);

  // If the field is not to be rendered by ds, just continue to the next field,
  // the content will come from another module (most likely CCK).
  if ($field_settings['pipeline'] = DS_RENDER_DEFAULT) {

    // Change the title if this is configured and label is not hidden.
    if (isset($field_settings['label_value']) && $field['labelformat'] != DS_DEFAULT_LABEL_FORMAT) {
      $field['title'] = t('%label', array(
        '%label' => $field_settings['label_value'],
      ));
    }

    // Add extra properties to be used in themeing.
    $field['key'] = $field_key;

    // Theming can either be done in preprocess, with a custom function or an
    // existing formatter theming function. Always pass the $field_settings as
    // parameter.
    // @todo: some of these should break earlier as no processing is required.
    switch ($field['field_type']) {
      case DS_FIELD_TYPE_PREPROCESS:
      case DS_FIELD_TYPE_IGNORE:
        if (isset($field_settings['properties']['key']) && !empty($field_settings['properties']['key'])) {
          $field['preprocess_settings'] = array(
            'type' => $field['type'],
            'key' => $field['properties']['key'],
          );
        }
        else {
          $field['preprocess_settings'] = array(
            'type' => $field['type'],
          );
        }
        break;
      case DS_FIELD_TYPE_CODE:
        $field['formatter'] = isset($field['format']) ? $field['format'] : 'ds_eval_code';
        break;
      case DS_FIELD_TYPE_BLOCK:
        $field['formatter'] = 'ds_eval_block';
        break;
      case DS_FIELD_TYPE_FUNCTION:
        $field['function'] = isset($field_settings['format']) ? $field_settings['format'] : key($field_settings['properties']['formatters']);
        break;
      case DS_FIELD_TYPE_THEME:
        $field['formatter'] = isset($field_settings['format']) ? $field_settings['format'] : key($field_settings['properties']['formatters']);
        break;
    }

    // Format content for the field.
    $field['content'] = ds_field_format_content($field);
  }
  return $field;
}