You are here

function template_preprocess_ingredient in Recipe 8.2

Prepares variables for ingredient templates.

Default template: ingredient.html.twig.

Parameters

array $variables: An associative array containing:

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

  • attributes: HTML attributes for the containing element.

File

modules/ingredient/ingredient.module, line 48
Contains basic functions for the Ingredient module.

Code

function template_preprocess_ingredient(array &$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['ingredient'] = $variables['elements']['#ingredient'];

  /** @var \Drupal\ingredient\IngredientInterface $ingredient */
  $ingredient = $variables['ingredient'];
  $variables['url'] = $ingredient
    ->toUrl()
    ->toString();

  // We use name here because that is what appears in the UI.
  $variables['name'] = $variables['elements']['name'];
  unset($variables['elements']['name']);
  $variables['page'] = $variables['view_mode'] == 'full' && ingredient_is_page($ingredient);

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