3 namespace Drupal\media;
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;
14 * Provides a listing of media items.
16 class MediaListBuilder extends EntityListBuilder {
19 * The date formatter service.
21 * @var \Drupal\Core\Datetime\DateFormatterInterface
23 protected $dateFormatter;
26 * The language manager service.
28 * @var \Drupal\Core\Language\LanguageManagerInterface
30 protected $languageManager;
33 * Indicates whether the 'thumbnail' image style exists.
37 protected $thumbnailStyleExists = FALSE;
40 * Constructs a new MediaListBuilder object.
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.
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);
56 $this->dateFormatter = $date_formatter;
57 $this->languageManager = $language_manager;
58 $this->thumbnailStyleExists = !empty($image_style_storage->load('thumbnail'));
64 public static function createInstance(ContainerInterface $container, EntityTypeInterface $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')
77 public function buildHeader() {
79 if ($this->thumbnailStyleExists) {
80 $header['thumbnail'] = [
81 'data' => $this->t('Thumbnail'),
82 'class' => [RESPONSIVE_PRIORITY_LOW],
86 'name' => $this->t('Media Name'),
88 'data' => $this->t('Type'),
89 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
92 'data' => $this->t('Author'),
93 'class' => [RESPONSIVE_PRIORITY_LOW],
95 'status' => $this->t('Status'),
97 'data' => $this->t('Updated'),
98 'class' => [RESPONSIVE_PRIORITY_LOW],
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],
108 return $header + parent::buildHeader();
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,
127 $row['name']['data'] = [
129 '#title' => $entity->label(),
130 '#url' => $entity->toUrl(),
132 $row['type'] = $entity->bundle->entity->label();
133 $row['author']['data'] = [
134 '#theme' => 'username',
135 '#account' => $entity->getOwner(),
137 $row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
138 $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
140 if ($this->languageManager->isMultilingual()) {
141 $row['language'] = $this->languageManager->getLanguageName($entity->language()->getId());
143 return $row + parent::buildRow($entity);
149 protected function getEntityIds() {
150 $query = $this->getStorage()->getQuery()
151 ->sort('changed', 'DESC');
153 // Only add the pager if a limit is specified.
155 $query->pager($this->limit);
157 return $query->execute();