Issue #2934424 by chr.fritsch, benjifisher, tstoeckler, phenaproxima, xjm, marcoscano, larowlan: Media has no collection route

8.5.x
Lee Rowlands 2018-01-10 07:26:04 +10:00
parent 0013e901fe
commit ba4f89edf0
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
6 changed files with 211 additions and 2 deletions

View File

@ -9,4 +9,4 @@ media.add:
title: 'Add media'
weight: 10
appears_on:
- view.media.media_page_list
- entity.media.collection

View File

@ -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

View File

@ -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.
}

View File

@ -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",

View File

@ -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();
}
}

View File

@ -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")');
}
}