You are here

function ds_extras_preprocess_field in Display Suite 7.2

Implements hook_preprocess_field().

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

File

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

Code

function ds_extras_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;
  }
  $entity_type = $variables['element']['#entity_type'];
  $bundle = $variables['element']['#bundle'];
  $view_mode = $variables['element']['#view_mode'];
  $config = array();
  static $field_settings = array();
  if (!isset($field_settings[$entity_type][$bundle][$view_mode])) {
    $layout = ds_get_layout($entity_type, $bundle, $view_mode);
    $field_settings[$entity_type][$bundle][$view_mode] = ds_get_field_settings($entity_type, $bundle, $view_mode);
  }

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

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

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

  // 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'][] = str_replace('theme_', '', $config['func']);
    $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'][] = str_replace('theme_', '', $field_instance_info['ds_extras_field_template']);
    $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'][] = str_replace('theme_', '', $field_theme_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'];
    }
  }

  // Sanitize the output of field templates
  $fields = array(
    'prefix',
    'suffix',
  );
  foreach ($fields as $field) {
    if (isset($variables['ds-config'][$field])) {
      $variables['ds-config'][$field] = filter_xss_admin($variables['ds-config'][$field]);
    }
  }
}