8.5.x theme.inc template_preprocess_field(&$variables, $hook)
8.0.x theme.inc template_preprocess_field(&$variables, $hook)
8.1.x theme.inc template_preprocess_field(&$variables, $hook)
8.2.x theme.inc template_preprocess_field(&$variables, $hook)
8.3.x theme.inc template_preprocess_field(&$variables, $hook)
8.4.x theme.inc template_preprocess_field(&$variables, $hook)
8.6.x theme.inc template_preprocess_field(&$variables, $hook)
7.x field.module template_preprocess_field(&$variables, $hook)

Prepares variables for field templates.

Default template: field.html.twig.

Parameters

array $variables: An associative array containing:

  • element: A render element representing the field.
  • attributes: A string containing the attributes for the wrapping div.
  • title_attributes: A string containing the attributes for the title.

File

core/includes/theme.inc, line 1531
The theme system, which controls the output of Drupal.

Code

function template_preprocess_field(&$variables, $hook) {
  $element = $variables['element'];

  // Creating variables for the template.
  $variables['entity_type'] = $element['#entity_type'];
  $variables['field_name'] = $element['#field_name'];
  $variables['field_type'] = $element['#field_type'];
  $variables['label_display'] = $element['#label_display'];
  $variables['label_hidden'] = $element['#label_display'] == 'hidden';

  // Always set the field label - allow themes to decide whether to display it.
  // In addition the label should be rendered but hidden to support screen
  // readers.
  $variables['label'] = $element['#title'];
  $variables['multiple'] = $element['#is_multiple'];
  static $default_attributes;
  if (!isset($default_attributes)) {
    $default_attributes = new Attribute();
  }

  // Merge attributes when a single-value field has a hidden label.
  if ($element['#label_display'] == 'hidden' && !$variables['multiple'] && !empty($element['#items'][0]->_attributes)) {
    $variables['attributes'] = NestedArray::mergeDeep($variables['attributes'], (array) $element['#items'][0]->_attributes);
  }

  // We want other preprocess functions and the theme implementation to have
  // fast access to the field item render arrays. The item render array keys
  // (deltas) should always be numerically indexed starting from 0, and looping
  // on those keys is faster than calling Element::children() or looping on all
  // keys within $element, since that requires traversal of all element
  // properties.
  $variables['items'] = [];
  $delta = 0;
  while (!empty($element[$delta])) {
    $variables['items'][$delta]['content'] = $element[$delta];

    // Modules (e.g., rdf.module) can add field item attributes (to
    // $item->_attributes) within hook_entity_prepare_view(). Some field
    // formatters move those attributes into some nested formatter-specific
    // element in order have them rendered on the desired HTML element (e.g., on
    // the <a> element of a field item being rendered as a link). Other field
    // formatters leave them within $element['#items'][$delta]['_attributes'] to
    // be rendered on the item wrappers provided by field.html.twig.
    $variables['items'][$delta]['attributes'] = !empty($element['#items'][$delta]->_attributes) ? new Attribute($element['#items'][$delta]->_attributes) : clone $default_attributes;
    $delta++;
  }
}