annotate core/modules/media/src/MediaListBuilder.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
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@5 65 $entity_type_manager = $container->get('entity_type.manager');
Chris@0 66 return new static(
Chris@0 67 $entity_type,
Chris@5 68 $entity_type_manager->getStorage($entity_type->id()),
Chris@0 69 $container->get('date.formatter'),
Chris@0 70 $container->get('language_manager'),
Chris@5 71 $entity_type_manager->getStorage('image_style')
Chris@0 72 );
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 public function buildHeader() {
Chris@0 79 $header = [];
Chris@0 80 if ($this->thumbnailStyleExists) {
Chris@0 81 $header['thumbnail'] = [
Chris@0 82 'data' => $this->t('Thumbnail'),
Chris@0 83 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 84 ];
Chris@0 85 }
Chris@0 86 $header += [
Chris@0 87 'name' => $this->t('Media Name'),
Chris@0 88 'type' => [
Chris@0 89 'data' => $this->t('Type'),
Chris@0 90 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
Chris@0 91 ],
Chris@0 92 'author' => [
Chris@0 93 'data' => $this->t('Author'),
Chris@0 94 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 95 ],
Chris@0 96 'status' => $this->t('Status'),
Chris@0 97 'changed' => [
Chris@0 98 'data' => $this->t('Updated'),
Chris@0 99 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 100 ],
Chris@0 101 ];
Chris@0 102 // Enable language column if multiple languages are added.
Chris@0 103 if ($this->languageManager->isMultilingual()) {
Chris@0 104 $header['language'] = [
Chris@0 105 'data' => $this->t('Language'),
Chris@0 106 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 107 ];
Chris@0 108 }
Chris@0 109 return $header + parent::buildHeader();
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * {@inheritdoc}
Chris@0 114 */
Chris@0 115 public function buildRow(EntityInterface $entity) {
Chris@0 116 /** @var \Drupal\media\MediaInterface $entity */
Chris@0 117 if ($this->thumbnailStyleExists) {
Chris@0 118 $row['thumbnail'] = [];
Chris@4 119 if ($thumbnail_uri = $entity->getSource()->getMetadata($entity, 'thumbnail_uri')) {
Chris@0 120 $row['thumbnail']['data'] = [
Chris@0 121 '#theme' => 'image_style',
Chris@0 122 '#style_name' => 'thumbnail',
Chris@4 123 '#uri' => $thumbnail_uri,
Chris@0 124 '#height' => 50,
Chris@0 125 ];
Chris@0 126 }
Chris@0 127 }
Chris@0 128 $row['name']['data'] = [
Chris@0 129 '#type' => 'link',
Chris@0 130 '#title' => $entity->label(),
Chris@0 131 '#url' => $entity->toUrl(),
Chris@0 132 ];
Chris@0 133 $row['type'] = $entity->bundle->entity->label();
Chris@0 134 $row['author']['data'] = [
Chris@0 135 '#theme' => 'username',
Chris@0 136 '#account' => $entity->getOwner(),
Chris@0 137 ];
Chris@0 138 $row['status'] = $entity->isPublished() ? $this->t('Published') : $this->t('Unpublished');
Chris@0 139 $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
Chris@0 140
Chris@0 141 if ($this->languageManager->isMultilingual()) {
Chris@0 142 $row['language'] = $this->languageManager->getLanguageName($entity->language()->getId());
Chris@0 143 }
Chris@0 144 return $row + parent::buildRow($entity);
Chris@0 145 }
Chris@0 146
Chris@0 147 /**
Chris@0 148 * {@inheritdoc}
Chris@0 149 */
Chris@0 150 protected function getEntityIds() {
Chris@0 151 $query = $this->getStorage()->getQuery()
Chris@0 152 ->sort('changed', 'DESC');
Chris@0 153
Chris@0 154 // Only add the pager if a limit is specified.
Chris@0 155 if ($this->limit) {
Chris@0 156 $query->pager($this->limit);
Chris@0 157 }
Chris@0 158 return $query->execute();
Chris@0 159 }
Chris@0 160
Chris@0 161 }