You are here

function fences_preprocess_field in Fences 7.2

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

Implements hook_preprocess_field().

File

./fences.module, line 171
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']);

  // Check if this is a ctools entity field with fence formatter.
  if (isset($variables['element']['#fences_wrapper'])) {
    $suggestion = $variables['element']['#fences_wrapper'];
  }

  // Optionally, override core's default markup.
  if (empty($suggestion)) {
    $suggestion = variable_get('fences_default_markup', 0) ? 'div' : 'div_div_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 !== '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'];
    }

    // If the field is using core's field template, add a class to the wrapper div for inline labels.
    if ($suggestion === 'div_div_div' && $variables['element']['#label_display'] === 'inline') {
      $variables['classes_array'][] = 'inline-sibling__wrapper';
    }
  }

  // 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.
  if ($variables['element']['#label_display'] == 'inline') {
    $variables['classes_array'] = array_diff($variables['classes_array'], array(
      'clearfix',
    ));
  }
}