5 * Theme functions for the media_entity module.
8 use Drupal\Core\Render\Element;
10 use Drupal\Component\Utility\Html;
13 * Prepares variables for list of available media bundles.
15 * Default template: media-add-list.html.twig.
17 * @param array $variables
18 * An associative array containing:
19 * - content: An array of content types.
21 function template_preprocess_media_add_list(&$variables) {
22 $variables['bundles'] = [];
23 if (!empty($variables['content'])) {
24 foreach ($variables['content'] as $bundle) {
25 /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
26 $variables['bundles'][$bundle->id()] = [
27 'type' => $bundle->id(),
28 'add_link' => Link::createFromRoute($bundle->label(), 'media.add', ['media_bundle' => $bundle->id()]),
30 '#markup' => $bundle->getDescription(),
38 * Prepares variables for media templates.
40 * Default template: media.html.twig.
42 * @param array $variables
43 * An associative array containing:
44 * - media: An individual media for display.
46 function template_preprocess_media(&$variables) {
47 /** @var \Drupal\media_entity\MediaInterface $media */
48 $media = $variables['elements']['#media'];
50 $variables['name'] = $media->label();
52 // Helpful $content variable for templates.
53 foreach (Element::children($variables['elements']) as $key) {
54 $variables['content'][$key] = $variables['elements'][$key];
57 $variables['attributes']['class'][] = 'media';
58 $variables['attributes']['class'][] = Html::getClass('media-' . $media->bundle());
59 if (!$media->isPublished()) {
60 $variables['attributes']['class'][] = 'unpublished';
62 if ($variables['elements']['#view_mode']) {
63 $variables['attributes']['class'][] = Html::getClass('view-mode-' . $variables['elements']['#view_mode']);