annotate core/modules/media/src/MediaListBuilder.php @ 4:a9cd425dd02b

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