summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLee Rowlands2018-01-09 21:26:04 (GMT)
committerLee Rowlands2018-01-09 21:26:04 (GMT)
commitba4f89edf015fbb635f47039e7d748a8af61c801 (patch)
tree2520912d6e77f81fc607826e5760d69aaec48591
parent0013e901feae75545e784b45f3f39a814fcac5c9 (diff)
Issue #2934424 by chr.fritsch, benjifisher, tstoeckler, phenaproxima, xjm, marcoscano, larowlan: Media has no collection route
-rw-r--r--core/modules/media/media.links.action.yml2
-rw-r--r--core/modules/media/media.links.task.yml6
-rw-r--r--core/modules/media/media.post_update.php13
-rw-r--r--core/modules/media/src/Entity/Media.php3
-rw-r--r--core/modules/media/src/MediaListBuilder.php160
-rw-r--r--core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php29
6 files changed, 211 insertions, 2 deletions
diff --git a/core/modules/media/media.links.action.yml b/core/modules/media/media.links.action.yml
index a056d35..e76ab06 100644
--- a/core/modules/media/media.links.action.yml
+++ b/core/modules/media/media.links.action.yml
@@ -9,4 +9,4 @@ media.add:
title: 'Add media'
weight: 10
appears_on:
- - view.media.media_page_list
+ - entity.media.collection
diff --git a/core/modules/media/media.links.task.yml b/core/modules/media/media.links.task.yml
index c7a669c..d71f378 100644
--- a/core/modules/media/media.links.task.yml
+++ b/core/modules/media/media.links.task.yml
@@ -23,3 +23,9 @@ entity.media_type.collection:
title: List
route_name: entity.media_type.collection
base_route: entity.media_type.collection
+
+entity.media.collection:
+ title: Media
+ route_name: entity.media.collection
+ base_route: system.admin_content
+ weight: 10
diff --git a/core/modules/media/media.post_update.php b/core/modules/media/media.post_update.php
new file mode 100644
index 0000000..2e49368
--- /dev/null
+++ b/core/modules/media/media.post_update.php
@@ -0,0 +1,13 @@
+<?php
+
+/**
+ * @file
+ * Post update functions for Media.
+ */
+
+/**
+ * Clear caches due to changes in local tasks and action links.
+ */
+function media_post_update_collection_route() {
+ // Empty post-update hook.
+}
diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php
index dc8e0dc..406b451 100644
--- a/core/modules/media/src/Entity/Media.php
+++ b/core/modules/media/src/Entity/Media.php
@@ -31,7 +31,7 @@ use Drupal\user\UserInterface;
* handlers = {
* "storage" = "Drupal\Core\Entity\Sql\SqlContentEntityStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
- * "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
+ * "list_builder" = "Drupal\media\MediaListBuilder",
* "access" = "Drupal\media\MediaAccessControlHandler",
* "form" = {
* "default" = "Drupal\media\MediaForm",
@@ -74,6 +74,7 @@ use Drupal\user\UserInterface;
* "add-page" = "/media/add",
* "add-form" = "/media/add/{media_type}",
* "canonical" = "/media/{media}",
+ * "collection" = "/admin/content/media",
* "delete-form" = "/media/{media}/delete",
* "edit-form" = "/media/{media}/edit",
* "revision" = "/media/{media}/revisions/{media_revision}/view",
diff --git a/core/modules/media/src/MediaListBuilder.php b/core/modules/media/src/MediaListBuilder.php
new file mode 100644
index 0000000..a0f1f02
--- /dev/null
+++ b/core/modules/media/src/MediaListBuilder.php
@@ -0,0 +1,160 @@
+<?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;
+
+/**
+ * Provides a listing of media items.
+ */
+class MediaListBuilder extends EntityListBuilder {
+
+ /**
+ * The date formatter service.
+ *
+ * @var \Drupal\Core\Datetime\DateFormatterInterface
+ */
+ protected $dateFormatter;
+
+ /**
+ * The language manager service.
+ *
+ * @var \Drupal\Core\Language\LanguageManagerInterface
+ */
+ protected $languageManager;
+
+ /**
+ * Indicates whether the 'thumbnail' image style exists.
+ *
+ * @var bool
+ */
+ protected $thumbnailStyleExists = FALSE;
+
+ /**
+ * Constructs a new MediaListBuilder object.
+ *
+ * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
+ * The entity type definition.
+ * @param \Drupal\Core\Entity\EntityStorageInterface $storage
+ * The entity storage class.
+ * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
+ * The date formatter service.
+ * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
+ * The language manager service.
+ * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
+ * The entity storage class for image styles.
+ */
+ 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'));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
+ return new static(
+ $entity_type,
+ $container->get('entity.manager')->getStorage($entity_type->id()),
+ $container->get('date.formatter'),
+ $container->get('language_manager'),
+ $container->get('entity_type.manager')->getStorage('image_style')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ 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],
+ ],
+ ];
+ // Enable language column if multiple languages are added.
+ if ($this->languageManager->isMultilingual()) {
+ $header['language'] = [
+ 'data' => $this->t('Language'),
+ 'class' => [RESPONSIVE_PRIORITY_LOW],
+ ];
+ }
+ return $header + parent::buildHeader();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildRow(EntityInterface $entity) {
+ /** @var \Drupal\media\MediaInterface $entity */
+ if ($this->thumbnailStyleExists) {
+ $row['thumbnail'] = [];
+ if ($thumbnail_url = $entity->getSource()->getMetadata($entity, 'thumbnail_uri')) {
+ $row['thumbnail']['data'] = [
+ '#theme' => 'image_style',
+ '#style_name' => 'thumbnail',
+ '#uri' => $thumbnail_url,
+ '#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);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getEntityIds() {
+ $query = $this->getStorage()->getQuery()
+ ->sort('changed', 'DESC');
+
+ // Only add the pager if a limit is specified.
+ if ($this->limit) {
+ $query->pager($this->limit);
+ }
+ return $query->execute();
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
index 8a0a82f..8d876b7 100644
--- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
+++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
@@ -192,4 +192,33 @@ class MediaUiFunctionalTest extends MediaFunctionalTestBase {
$assert_session->fieldValueEquals('fields[field_foo_field][type]', 'entity_reference_entity_view');
}
+ /**
+ * Test the media collection route.
+ */
+ public function testMediaCollectionRoute() {
+ /** @var \Drupal\Core\Entity\EntityStorageInterface $media_storage */
+ $media_storage = $this->container->get('entity_type.manager')->getStorage('media');
+
+ $this->container->get('module_installer')->uninstall(['views']);
+
+ // Create a media type and media item.
+ $media_type = $this->createMediaType();
+ $media = $media_storage->create([
+ 'bundle' => $media_type->id(),
+ 'name' => 'Unnamed',
+ ]);
+ $media->save();
+
+ $this->drupalGet($media->toUrl('collection'));
+
+ $assert_session = $this->assertSession();
+
+ // Media list table exists.
+ $assert_session->elementExists('css', 'th:contains("Media Name")');
+ $assert_session->elementExists('css', 'th:contains("Type")');
+ $assert_session->elementExists('css', 'th:contains("Operations")');
+ // Media item is present.
+ $assert_session->elementExists('css', 'td:contains("Unnamed")');
+ }
+
}