Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / media / src / MediaListBuilder.php
1 <?php
2
3 namespace Drupal\media;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityListBuilder;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a listing of media items.
15  */
16 class MediaListBuilder extends EntityListBuilder {
17
18   /**
19    * The date formatter service.
20    *
21    * @var \Drupal\Core\Datetime\DateFormatterInterface
22    */
23   protected $dateFormatter;
24
25   /**
26    * The language manager service.
27    *
28    * @var \Drupal\Core\Language\LanguageManagerInterface
29    */
30   protected $languageManager;
31
32   /**
33    * Indicates whether the 'thumbnail' image style exists.
34    *
35    * @var bool
36    */
37   protected $thumbnailStyleExists = FALSE;
38
39   /**
40    * Constructs a new MediaListBuilder object.
41    *
42    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
43    *   The entity type definition.
44    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
45    *   The entity storage class.
46    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
47    *   The date formatter service.
48    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
49    *   The language manager service.
50    * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
51    *   The entity storage class for image styles.
52    */
53   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager, EntityStorageInterface $image_style_storage) {
54     parent::__construct($entity_type, $storage);
55
56     $this->dateFormatter = $date_formatter;
57     $this->languageManager = $language_manager;
58     $this->thumbnailStyleExists = !empty($image_style_storage->load('thumbnail'));
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
65     return new static(
66       $entity_type,
67       $container->get('entity.manager')->getStorage($entity_type->id()),
68       $container->get('date.formatter'),
69       $container->get('language_manager'),
70       $container->get('entity_type.manager')->getStorage('image_style')
71     );
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildHeader() {
78     $header = [];
79     if ($this->thumbnailStyleExists) {
80       $header['thumbnail'] = [
81         'data' => $this->t('Thumbnail'),
82         'class' => [RESPONSIVE_PRIORITY_LOW],
83       ];
84     }
85     $header += [
86       'name' => $this->t('Media Name'),
87       'type' => [
88         'data' => $this->t('Type'),
89         'class' => [RESPONSIVE_PRIORITY_MEDIUM],
90       ],
91       'author' => [
92         'data' => $this->t('Author'),
93         'class' => [RESPONSIVE_PRIORITY_LOW],
94       ],
95       'status' => $this->t('Status'),
96       'changed' => [
97         'data' => $this->t('Updated'),
98         'class' => [RESPONSIVE_PRIORITY_LOW],
99       ],
100     ];
101     // Enable language column if multiple languages are added.
102     if ($this->languageManager->isMultilingual()) {
103       $header['language'] = [
104         'data' => $this->t('Language'),
105         'class' => [RESPONSIVE_PRIORITY_LOW],
106       ];
107     }
108     return $header + parent::buildHeader();
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function buildRow(EntityInterface $entity) {
115     /** @var \Drupal\media\MediaInterface $entity */
116     if ($this->thumbnailStyleExists) {
117       $row['thumbnail'] = [];
118       if ($thumbnail_uri = $entity->getSource()->getMetadata($entity, 'thumbnail_uri')) {
119         $row['thumbnail']['data'] = [
120           '#theme' => 'image_style',
121           '#style_name' => 'thumbnail',
122           '#uri' => $thumbnail_uri,
123           '#height' => 50,
124         ];
125       }
126     }
127     $row['name']['data'] = [
128       '#type' => 'link',
129       '#title' => $entity->label(),
130       '#url' => $entity->toUrl(),
131     ];
132     $row['type'] = $entity->bundle->entity->label();
133     $row['author']['data'] = [
134       '#theme' => 'username',
135       '#account' => $entity->getOwner(),
136     ];
137     $row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
138     $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
139
140     if ($this->languageManager->isMultilingual()) {
141       $row['language'] = $this->languageManager->getLanguageName($entity->language()->getId());
142     }
143     return $row + parent::buildRow($entity);
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   protected function getEntityIds() {
150     $query = $this->getStorage()->getQuery()
151       ->sort('changed', 'DESC');
152
153     // Only add the pager if a limit is specified.
154     if ($this->limit) {
155       $query->pager($this->limit);
156     }
157     return $query->execute();
158   }
159
160 }