You are here

function ds_preprocess_field in Display Suite 7

Same name and namespace in other branches
  1. 8.4 ds.module \ds_preprocess_field()
  2. 8.2 ds.module \ds_preprocess_field()
  3. 8.3 ds.module \ds_preprocess_field()

Implements hook_preprocess_field().

1 string reference to 'ds_preprocess_field'
_ds_extras_theme_registry_alter in modules/ds_extras/ds_extras.registry.inc
Implements hook_theme_registry_alter().

File

modules/ds_extras/ds_extras.module, line 295
Display Suite extras main functions.

Code

function ds_preprocess_field(&$variables) {

  // We need to be sure this field is in a layout which is rendered by DS.
  if (!ds_get_layout($variables['element']['#entity_type'], $variables['element']['#bundle'], $variables['element']['#view_mode'])) {
    return;
  }
  $config = array();
  static $field_settings = array();
  if (!isset($field_settings[$variables['element']['#entity_type']][$variables['element']['#bundle']][$variables['element']['#view_mode']])) {
    $layout = ds_get_layout($variables['element']['#entity_type'], $variables['element']['#bundle'], $variables['element']['#view_mode']);
    $field_settings[$variables['element']['#entity_type']][$variables['element']['#bundle']][$variables['element']['#view_mode']] = ds_get_field_settings($variables['element']['#entity_type'], $variables['element']['#bundle'], $layout['view_mode']);
  }

  // Get the field name and field instance info - if available.
  $field_name = $variables['element']['#field_name'];
  $field_instance_info = field_info_instance($variables['element']['#entity_type'], $field_name, $variables['element']['#bundle']);

  // Check if this field has custom output settings.
  $variables['ds-config'] = array();
  if (isset($field_settings[$variables['element']['#entity_type']][$variables['element']['#bundle']][$variables['element']['#view_mode']][$field_name]['ft'])) {
    $config = $field_settings[$variables['element']['#entity_type']][$variables['element']['#bundle']][$variables['element']['#view_mode']][$field_name]['ft'];
    $variables['ds-config'] = $config;
  }

  // Styles
  if (isset($config['styles'])) {
    $variables['classes_array'][] = $config['styles'];
  }

  // Alter the label if configured.
  if (!$variables['label_hidden']) {
    if (isset($config['lb'])) {
      $variables['label'] = t(check_plain($config['lb']));
    }
  }

  // Determine the field template. In case it's something different
  // than theme_field, we'll add that function as a suggestion.
  if (isset($config['func']) && $config['func'] != 'theme_field') {
    $variables['ds-config'] = $config;
    $variables['theme_hook_suggestions'] = array();

    // Either it uses the function.
    $variables['theme_hook_suggestions'][] = $config['func'];

    // Or the template file(s).
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $config['func']);
    $variables['theme_hook_suggestions'][] = $suggestion;
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
  }
  elseif (isset($field_instance_info['ds_extras_field_template']) && !empty($field_instance_info['ds_extras_field_template']) && $field_instance_info['ds_extras_field_template'] != 'theme_field') {
    $variables['theme_hook_suggestions'] = array();

    // Either it uses the function.
    $variables['theme_hook_suggestions'][] = $field_instance_info['ds_extras_field_template'];

    // Or the template file(s).
    $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_instance_info['ds_extras_field_template']);
    $variables['theme_hook_suggestions'][] = $suggestion;
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
    $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
  }
  elseif (!isset($config['func']) || empty($config['func'])) {
    $field_theme_function = variable_get('ft-default', 'theme_field');
    if ($field_theme_function != 'theme_field') {
      $variables['theme_hook_suggestions'] = array();
      $variables['ds-config'] = $config;

      // Either it uses the function.
      $variables['theme_hook_suggestions'][] = $field_theme_function;

      // Or the template file(s).
      $suggestion = 'field__' . str_replace('theme_ds_field_', '', $field_theme_function);
      $variables['theme_hook_suggestions'][] = $suggestion;
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name;
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $variables['element']['#bundle'];
      $variables['theme_hook_suggestions'][] = $suggestion . '__' . $field_name . '__' . $variables['element']['#bundle'];
    }
  }
}