You are here

function fences_preprocess_field in Fences 7

Same name and namespace in other branches
  1. 8.2 fences.module \fences_preprocess_field()
  2. 7.2 fences.module \fences_preprocess_field()

Implements hook_preprocess_field().

File

./fences.module, line 163
Fences is a module providing configurable field wrappers.

Code

function fences_preprocess_field(&$variables) {

  // Retrieve the configured suggestion.
  $suggestion = fences_get_suggestion($variables['element']['#entity_type'], $variables['element']['#bundle'], $variables['element']['#field_name']);

  // Optionally, override core's default markup.
  if (empty($suggestion) && variable_get('fences_default_markup', 0)) {
    $suggestion = 'div';
  }

  // Add a theme hook suggestion for the configured suggestion, unless the
  // suggestion is "div_div_div", in which case we should use Drupal's default
  // field markup instead of a theme hook suggestion.
  if ($suggestion && $suggestion !== 'div_div_div') {

    // Make fences' suggestions low priority by placing them at the front of the queue.
    $suggestion = 'field__fences_' . $suggestion;
    if (count($variables['items']) > 1) {

      // Add a "-multiple" suggestion if there are more than one field items.
      array_unshift($variables['theme_hook_suggestions'], $suggestion . '_multiple');
    }
    array_unshift($variables['theme_hook_suggestions'], $suggestion);
  }
  if (variable_get('fences_default_classes', 0)) {

    // Remove all of core's default classes.
    $variables['classes_array'] = array_diff($variables['classes_array'], array(
      'field',
      'field-name-' . $variables['field_name_css'],
      'field-type-' . $variables['field_type_css'],
      'field-label-' . $variables['element']['#label_display'],
    ));

    // Add our lean field class.
    if (strpos($variables['field_name_css'], 'field-') === 0) {
      $variables['classes_array'][] = $variables['field_name_css'];
    }
    else {
      $variables['classes_array'][] = 'field-' . $variables['field_name_css'];
    }
  }

  // Core adds a "clearfix" class to the wrapper since it floats the label and
  // the field items in its field.css if the label is inline. That's nonsense,
  // so we remove it and add a simple, unstyled "inline" class.
  if ($variables['element']['#label_display'] == 'inline') {
    $variables['classes_array'] = array_diff($variables['classes_array'], array(
      'clearfix',
    ));
    $variables['classes_array'][] = 'inline';
  }
}