annotate core/modules/media/src/MediaListBuilder.php @ 15:e200cb7efeb3

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