function template_preprocess_taxonomy_term

Prepares variables for taxonomy term templates.

Default template: taxonomy-term.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An associative array containing the taxonomy term and any fields attached to the term. Properties used:

    • #term: The term object.
    • #view_mode: The current view mode for this taxonomy term, e.g. 'full' or 'teaser'.
  • attributes: HTML attributes for the containing element.

File

drupal/core/modules/taxonomy/taxonomy.module, line 465
Enables the organization of content into categories.

Code

function template_preprocess_taxonomy_term(&$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['term'] = $variables['elements']['#term'];
  $term = $variables['term'];
  $uri = $term
    ->uri();
  $variables['url'] = url($uri['path'], $uri['options']);

  // We use name here because that is what appears in the UI.
  $variables['name'] = check_plain($term
    ->label());
  $variables['page'] = $variables['view_mode'] == 'full' && taxonomy_term_is_page($term);

  // Helpful $content variable for templates.
  $variables['content'] = array();
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // field_attach_preprocess() overwrites the $[field_name] variables with the
  // values of the field in the language that was selected for display, instead
  // of the raw values in $term->[field_name], which contain all values in all
  // languages.
  field_attach_preprocess($term, $variables['content'], $variables);

  // Gather classes, and clean up name so there are no underscores.
  $variables['attributes']['class'][] = 'taxonomy-term';
  $vocabulary_name_css = str_replace('_', '-', $term
    ->bundle());
  $variables['attributes']['class'][] = 'vocabulary-' . $vocabulary_name_css;
  $variables['theme_hook_suggestions'][] = 'taxonomy_term__' . $term
    ->bundle();
  $variables['theme_hook_suggestions'][] = 'taxonomy_term__' . $term
    ->id();
}