function TaxonomyThemeHooks::preprocessTaxonomyTerm

Prepares variables for taxonomy term templates.

Default template: taxonomy-term.html.twig.

By default this function performs special preprocessing to move the name base field out of the elements array into a separate variable. This preprocessing is skipped if:

  • a module makes the field's display configurable via the field UI by means of BaseFieldDefinition::setDisplayConfigurable()
  • AND the additional entity type property 'enable_base_field_custom_preprocess_skipping' has been set using hook_entity_type_build().

Parameters

array $variables: An associative array containing:

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

  • attributes: HTML attributes for the containing element.

File

core/modules/taxonomy/src/Hook/TaxonomyThemeHooks.php, line 67

Class

TaxonomyThemeHooks
Hook implementations for taxonomy.

Namespace

Drupal\taxonomy\Hook

Code

public function preprocessTaxonomyTerm(array &$variables) : void {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['term'] = $variables['elements']['#taxonomy_term'];
  /** @var \Drupal\taxonomy\TermInterface $term */
  $term = $variables['term'];
  $variables['url'] = !$term->isNew() ? $term->toUrl()
    ->toString() : NULL;
  // Make name field available separately.  Skip this custom preprocessing if
  // the field display is configurable and skipping has been enabled.
  // @todo https://www.drupal.org/project/drupal/issues/3015623
  //   Eventually delete this code and matching template lines. Using
  //   $variables['content'] is more flexible and consistent.
  $skip_custom_preprocessing = $term->getEntityType()
    ->get('enable_base_field_custom_preprocess_skipping');
  if (!$skip_custom_preprocessing || !$term->getFieldDefinition('name')
    ->isDisplayConfigurable('view')) {
    // We use name here because that is what appears in the UI.
    $variables['name'] = $variables['elements']['name'];
    unset($variables['elements']['name']);
  }
  // The page variable is deprecated.
  $variables['deprecations']['page'] = "'page' is deprecated in drupal:11.3.0 and is removed in drupal:13.0.0. Use 'view_mode' instead. See https://www.drupal.org/node/3542527";
  $variables['page'] = FALSE;
  if ($variables['view_mode'] == 'full' && ($this->routeMatch
    ->getRouteName() == 'entity.taxonomy_term.canonical' && $this->routeMatch
    ->getRawParameter('taxonomy_term') == $term->id())) {
    $variables['page'] = TRUE;
  }
  // Helpful $content variable for templates.
  $variables['content'] = [];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.