You are here

function template_preprocess_media in Media entity 8

Prepares variables for media templates.

Default template: media.html.twig.

Parameters

array $variables: An associative array containing:

  • media: An individual media for display.

File

./media_entity.theme.inc, line 46
Theme functions for the media_entity module.

Code

function template_preprocess_media(&$variables) {

  /** @var \Drupal\media_entity\MediaInterface $media */
  $media = $variables['elements']['#media'];
  $variables['name'] = $media
    ->label();

  // Helpful $content variable for templates.
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
  $variables['attributes']['class'][] = 'media';
  $variables['attributes']['class'][] = Html::getClass('media-' . $media
    ->bundle());
  if (!$media
    ->isPublished()) {
    $variables['attributes']['class'][] = 'unpublished';
  }
  if ($variables['elements']['#view_mode']) {
    $variables['attributes']['class'][] = Html::getClass('view-mode-' . $variables['elements']['#view_mode']);
  }
}