annotate core/modules/media/src/MediaListBuilder.php @ 19:fa3358dc1485 tip

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