605348ca63a24e21b45be6bcf44a323a8af3eae2
[yaffs-website] / web / modules / contrib / media_entity / media_entity.theme.inc
1 <?php
2
3 /**
4  * @file
5  * Theme functions for the media_entity module.
6  */
7
8 use Drupal\Core\Render\Element;
9 use Drupal\Core\Link;
10 use Drupal\Component\Utility\Html;
11
12 /**
13  * Prepares variables for list of available media bundles.
14  *
15  * Default template: media-add-list.html.twig.
16  *
17  * @param array $variables
18  *   An associative array containing:
19  *   - content: An array of content types.
20  */
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()]),
29         'description' => [
30           '#markup' => $bundle->getDescription(),
31         ],
32       ];
33     }
34   }
35 }
36
37 /**
38  * Prepares variables for media templates.
39  *
40  * Default template: media.html.twig.
41  *
42  * @param array $variables
43  *   An associative array containing:
44  *   - media: An individual media for display.
45  */
46 function template_preprocess_media(&$variables) {
47   /** @var \Drupal\media_entity\MediaInterface $media */
48   $media = $variables['elements']['#media'];
49
50   $variables['name'] = $media->label();
51
52   // Helpful $content variable for templates.
53   foreach (Element::children($variables['elements']) as $key) {
54     $variables['content'][$key] = $variables['elements'][$key];
55   }
56
57   $variables['attributes']['class'][] = 'media';
58   $variables['attributes']['class'][] = Html::getClass('media-' . $media->bundle());
59   if (!$media->isPublished()) {
60     $variables['attributes']['class'][] = 'unpublished';
61   }
62   if ($variables['elements']['#view_mode']) {
63     $variables['attributes']['class'][] = Html::getClass('view-mode-' . $variables['elements']['#view_mode']);
64   }
65 }