You are here

function template_preprocess_media in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media/media.module \template_preprocess_media()
  2. 9 core/modules/media/media.module \template_preprocess_media()

Prepares variables for media templates.

Default template: media.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • media: The media item.
  • name: The label for the media item.
  • view_mode: View mode; e.g., 'full', 'teaser', etc.

File

core/modules/media/media.module, line 159
Provides media items.

Code

function template_preprocess_media(array &$variables) {
  $variables['media'] = $variables['elements']['#media'];
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['name'] = $variables['media']
    ->label();

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