View source
<?php
namespace Drupal\media;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaListBuilder extends EntityListBuilder {
protected $dateFormatter;
protected $languageManager;
protected $thumbnailStyleExists = FALSE;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, LanguageManagerInterface $language_manager, EntityStorageInterface $image_style_storage) {
parent::__construct($entity_type, $storage);
$this->dateFormatter = $date_formatter;
$this->languageManager = $language_manager;
$this->thumbnailStyleExists = !empty($image_style_storage
->load('thumbnail'));
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type, $entity_type_manager
->getStorage($entity_type
->id()), $container
->get('date.formatter'), $container
->get('language_manager'), $entity_type_manager
->getStorage('image_style'));
}
public function buildHeader() {
$header = [];
if ($this->thumbnailStyleExists) {
$header['thumbnail'] = [
'data' => $this
->t('Thumbnail'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
}
$header += [
'name' => $this
->t('Media Name'),
'type' => [
'data' => $this
->t('Type'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
'author' => [
'data' => $this
->t('Author'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
'status' => $this
->t('Status'),
'changed' => [
'data' => $this
->t('Updated'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
];
if ($this->languageManager
->isMultilingual()) {
$header['language'] = [
'data' => $this
->t('Language'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
}
return $header + parent::buildHeader();
}
public function buildRow(EntityInterface $entity) {
if ($this->thumbnailStyleExists) {
$row['thumbnail'] = [];
if ($thumbnail_uri = $entity
->getSource()
->getMetadata($entity, 'thumbnail_uri')) {
$row['thumbnail']['data'] = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $thumbnail_uri,
'#height' => 50,
];
}
}
$row['name']['data'] = [
'#type' => 'link',
'#title' => $entity
->label(),
'#url' => $entity
->toUrl(),
];
$row['type'] = $entity->bundle->entity
->label();
$row['author']['data'] = [
'#theme' => 'username',
'#account' => $entity
->getOwner(),
];
$row['status'] = $entity
->isPublished() ? $this
->t('Published') : $this
->t('Unpublished');
$row['changed'] = $this->dateFormatter
->format($entity
->getChangedTime(), 'short');
if ($this->languageManager
->isMultilingual()) {
$row['language'] = $this->languageManager
->getLanguageName($entity
->language()
->getId());
}
return $row + parent::buildRow($entity);
}
protected function getEntityIds() {
$query = $this
->getStorage()
->getQuery()
->accessCheck(TRUE)
->sort('changed', 'DESC');
if ($this->limit) {
$query
->pager($this->limit);
}
return $query
->execute();
}
}