updated core from 8.4 to 8.5 : bug with login_destination

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-13 14:01:21 +01:00
parent 0d78da249b
commit e668535a4e
3486 changed files with 89604 additions and 33283 deletions
@@ -91,7 +91,7 @@ class MediaSource extends Plugin {
/**
* (optional) The metadata attribute name to provide the thumbnail title.
*
* The name of the media entity will be used if the attribute name is not
* The name of the media item will be used if the attribute name is not
* provided.
*
* @var string|null
+4 -8
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",
@@ -66,7 +66,7 @@ use Drupal\user\UserInterface;
* "revision_log_message" = "revision_log_message",
* },
* bundle_entity_type = "media_type",
* permission_granularity = "entity_type",
* permission_granularity = "bundle",
* admin_permission = "administer media",
* field_ui_base_route = "entity.media_type.edit_form",
* common_reference_target = TRUE,
@@ -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",
@@ -404,12 +405,7 @@ class Media extends EditorialContentEntityBase implements MediaInterface {
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
]);
->setDisplayConfigurable('view', TRUE);
$fields['thumbnail'] = BaseFieldDefinition::create('image')
->setLabel(t('Thumbnail'))
@@ -21,6 +21,7 @@ use Drupal\media\MediaTypeInterface;
* plural = "@count media types"
* ),
* handlers = {
* "access" = "Drupal\media\MediaTypeAccessControlHandler",
* "form" = {
* "add" = "Drupal\media\MediaTypeForm",
* "edit" = "Drupal\media\MediaTypeForm",
@@ -6,12 +6,14 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\PrivateTempStoreFactory;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Provides a confirmation form to delete multiple media items at once.
*
* @internal
*/
class MediaDeleteMultipleConfirmForm extends ConfirmFormBase {
@@ -25,7 +27,7 @@ class MediaDeleteMultipleConfirmForm extends ConfirmFormBase {
/**
* The tempstore factory.
*
* @var \Drupal\user\PrivateTempStoreFactory
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
protected $tempStoreFactory;
@@ -39,7 +41,7 @@ class MediaDeleteMultipleConfirmForm extends ConfirmFormBase {
/**
* Constructs a MediaDeleteMultipleConfirmForm form object.
*
* @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The tempstore factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
* The entity type manager.
@@ -54,7 +56,7 @@ class MediaDeleteMultipleConfirmForm extends ConfirmFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('user.private_tempstore'),
$container->get('tempstore.private'),
$container->get('entity_type.manager')
);
}
@@ -9,6 +9,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for media type deletion.
*
* @internal
*/
class MediaTypeDeleteConfirmForm extends EntityDeleteForm {
@@ -8,7 +8,7 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Defines an access control handler for the media entity.
* Defines an access control handler for media items.
*/
class MediaAccessControlHandler extends EntityAccessControlHandler {
@@ -20,6 +20,7 @@ class MediaAccessControlHandler extends EntityAccessControlHandler {
return AccessResult::allowed()->cachePerPermissions();
}
$type = $entity->bundle();
$is_owner = ($account->id() && $account->id() === $entity->getOwnerId());
switch ($operation) {
case 'view':
@@ -32,22 +33,38 @@ class MediaAccessControlHandler extends EntityAccessControlHandler {
return $access_result;
case 'update':
if ($account->hasPermission('edit any ' . $type . ' media')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($account->hasPermission('edit own ' . $type . ' media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
// @todo Deprecate this permission in
// https://www.drupal.org/project/drupal/issues/2925459.
if ($account->hasPermission('update any media')) {
return AccessResult::allowed()->cachePerPermissions();
}
return AccessResult::allowedIf($account->hasPermission('update media') && $is_owner)
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($entity);
if ($account->hasPermission('update media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
return AccessResult::neutral()->cachePerPermissions();
case 'delete':
if ($account->hasPermission('delete any ' . $type . ' media')) {
return AccessResult::allowed()->cachePerPermissions();
}
if ($account->hasPermission('delete own ' . $type . ' media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
// @todo Deprecate this permission in
// https://www.drupal.org/project/drupal/issues/2925459.
if ($account->hasPermission('delete any media')) {
return AccessResult::allowed()->cachePerPermissions();
}
return AccessResult::allowedIf($account->hasPermission('delete media') && $is_owner)
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($entity);
if ($account->hasPermission('delete media') && $is_owner) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
}
return AccessResult::neutral()->cachePerPermissions();
default:
return AccessResult::neutral()->cachePerPermissions();
@@ -58,7 +75,12 @@ class MediaAccessControlHandler extends EntityAccessControlHandler {
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermissions($account, ['administer media', 'create media'], 'OR');
$permissions = [
'administer media',
'create media',
'create ' . $entity_bundle . ' media',
];
return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
}
}
+2
View File
@@ -7,6 +7,8 @@ use Drupal\Core\Form\FormStateInterface;
/**
* Form controller for the media edit forms.
*
* @internal
*/
class MediaForm extends ContentEntityForm {
+160
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();
}
}
@@ -0,0 +1,92 @@
<?php
namespace Drupal\media;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides dynamic permissions for each media type.
*/
class MediaPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* MediaPermissions constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* Returns an array of media type permissions.
*
* @return array
* The media type permissions.
*
* @see \Drupal\user\PermissionHandlerInterface::getPermissions()
*/
public function mediaTypePermissions() {
$perms = [];
// Generate media permissions for all media types.
$media_types = $this->entityTypeManager
->getStorage('media_type')->loadMultiple();
foreach ($media_types as $type) {
$perms += $this->buildPermissions($type);
}
return $perms;
}
/**
* Returns a list of media permissions for a given media type.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type.
*
* @return array
* An associative array of permission names and descriptions.
*/
protected function buildPermissions(MediaTypeInterface $type) {
$type_id = $type->id();
$type_params = ['%type_name' => $type->label()];
return [
"create $type_id media" => [
'title' => $this->t('%type_name: Create new media', $type_params),
],
"edit own $type_id media" => [
'title' => $this->t('%type_name: Edit own media', $type_params),
],
"edit any $type_id media" => [
'title' => $this->t('%type_name: Edit any media', $type_params),
],
"delete own $type_id media" => [
'title' => $this->t('%type_name: Delete own media', $type_params),
],
"delete any $type_id media" => [
'title' => $this->t('%type_name: Delete any media', $type_params),
],
];
}
}
@@ -3,6 +3,8 @@
namespace Drupal\media;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
@@ -317,4 +319,37 @@ abstract class MediaSourceBase extends PluginBase implements MediaSourceInterfac
return $id;
}
/**
* {@inheritdoc}
*/
public function getSourceFieldValue(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
if (empty($source_field)) {
throw new \RuntimeException('Source field for media source is not defined.');
}
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
$field_item = $media->get($source_field)->first();
return $field_item->{$field_item->mainPropertyName()};
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName());
}
/**
* {@inheritdoc}
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
// Make sure the source field is placed just after the "name" basefield.
$name_component = $display->getComponent('name');
$source_field_weight = ($name_component && isset($name_component['weight'])) ? $name_component['weight'] + 5 : -50;
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'weight' => $source_field_weight,
]);
}
}
@@ -4,6 +4,8 @@ namespace Drupal\media;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Plugin\PluginFormInterface;
/**
@@ -23,7 +25,7 @@ use Drupal\Core\Plugin\PluginFormInterface;
* - Image: handles local images,
* - oEmbed: handles resources that are exposed through the oEmbed standard,
* - YouTube: handles YouTube videos,
* - SoundCould: handles SoundCloud audio,
* - SoundCloud: handles SoundCloud audio,
* - Instagram: handles Instagram posts,
* - Twitter: handles tweets,
* - ...
@@ -139,4 +141,54 @@ interface MediaSourceInterface extends PluginInspectionInterface, ConfigurablePl
*/
public function createSourceField(MediaTypeInterface $type);
/**
* Prepares the media type fields for this source in the view display.
*
* This method should normally call
* \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() or
* \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() to
* configure the media type fields in the view display.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type which is using this source.
* @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
* The display which should be prepared.
*
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent()
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent()
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display);
/**
* Prepares the media type fields for this source in the form display.
*
* This method should normally call
* \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent() or
* \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent() to
* configure the media type fields in the form display.
*
* @param \Drupal\media\MediaTypeInterface $type
* The media type which is using this source.
* @param \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display
* The display which should be prepared.
*
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::setComponent()
* @see \Drupal\Core\Entity\Display\EntityDisplayInterface::removeComponent()
*/
public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display);
/**
* Get the primary value stored in the source field.
*
* @param MediaInterface $media
* A media item.
*
* @return mixed
* The source value.
*
* @throws \RuntimeException
* If the source field for the media source is not defined.
*/
public function getSourceFieldValue(MediaInterface $media);
}
@@ -0,0 +1,34 @@
<?php
namespace Drupal\media;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Defines the access control handler for the "Media Type" entity type.
*
* @see \Drupal\media\Entity\MediaType
*/
class MediaTypeAccessControlHandler extends EntityAccessControlHandler {
/**
* {@inheritdoc}
*/
protected $viewLabelOperation = TRUE;
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'view label') {
return AccessResult::allowedIfHasPermission($account, 'view media');
}
else {
return parent::checkAccess($entity, $operation, $account);
}
}
}
+39 -20
View File
@@ -15,6 +15,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form controller for media type forms.
*
* @internal
*/
class MediaTypeForm extends EntityForm {
@@ -116,14 +118,30 @@ class MediaTypeForm extends EntityForm {
'#attributes' => ['id' => 'source-dependent'],
];
if ($source) {
$source_description = $this->t('<em>The media source cannot be changed after the media type is created.</em>');
}
else {
$source_description = $this->t('Media source that is responsible for additional logic related to this media type.');
}
$form['source_dependent']['source'] = [
'#type' => 'select',
'#title' => $this->t('Media source'),
'#default_value' => $source ? $source->getPluginId() : NULL,
'#options' => $options,
'#description' => $this->t('Media source that is responsible for additional logic related to this media type.'),
'#description' => $source_description,
'#ajax' => ['callback' => '::ajaxHandlerData'],
// Rebuilding the form as part of the AJAX request is a workaround to
// enforce machine_name validation.
// @todo This was added as part of #2932226 and it should be removed once
// https://www.drupal.org/project/drupal/issues/2557299 solves it in a
// more generic way.
'#executes_submit_callback' => TRUE,
'#submit' => [[static::class, 'rebuildSubmit']],
'#required' => TRUE,
// Once the media type is created, its source plugin cannot be changed
// anymore.
'#disabled' => !empty($source),
];
if (!$source) {
@@ -222,6 +240,18 @@ class MediaTypeForm extends EntityForm {
return $form;
}
/**
* Form submission handler to rebuild the form on select submit.
*
* @param array $form
* Full form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Current form state.
*/
public static function rebuildSubmit(array &$form, FormStateInterface $form_state) {
$form_state->setRebuild();
}
/**
* Prepares workflow options to be used in the 'checkboxes' form element.
*
@@ -262,7 +292,7 @@ class MediaTypeForm extends EntityForm {
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if ($form['source_dependent']['source_configuration']) {
if (isset($form['source_dependent']['source_configuration'])) {
// Let the selected plugin validate its settings.
$this->entity->getSource()->validateConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
}
@@ -285,7 +315,7 @@ class MediaTypeForm extends EntityForm {
->setStatus((bool) $form_state->getValue(['options', 'status']))
->setNewRevision((bool) $form_state->getValue(['options', 'new_revision']));
if ($form['source_dependent']['source_configuration']) {
if (isset($form['source_dependent']['source_configuration'])) {
// Let the selected plugin save its settings.
$this->entity->getSource()->submitConfigurationForm($form['source_dependent']['source_configuration'], $this->getSourceSubFormState($form, $form_state));
}
@@ -325,30 +355,19 @@ class MediaTypeForm extends EntityForm {
// Add the new field to the default form and view displays for this
// media type.
$field_name = $source_field->getName();
$field_type = $source_field->getType();
if ($source_field->isDisplayConfigurable('form')) {
// Use the default widget and settings.
$component = \Drupal::service('plugin.manager.field.widget')
->prepareConfiguration($field_type, []);
// @todo Replace entity_get_form_display() when #2367933 is done.
// https://www.drupal.org/node/2872159.
entity_get_form_display('media', $media_type->id(), 'default')
->setComponent($field_name, $component)
->save();
$display = entity_get_form_display('media', $media_type->id(), 'default');
$source->prepareFormDisplay($media_type, $display);
$display->save();
}
if ($source_field->isDisplayConfigurable('view')) {
// Use the default formatter and settings.
$component = \Drupal::service('plugin.manager.field.formatter')
->prepareConfiguration($field_type, []);
// @todo Replace entity_get_display() when #2367933 is done.
// https://www.drupal.org/node/2872159.
entity_get_display('media', $media_type->id(), 'default')
->setComponent($field_name, $component)
->save();
$display = entity_get_display('media', $media_type->id(), 'default');
$source->prepareViewDisplay($media_type, $display);
$display->save();
}
}
@@ -100,7 +100,7 @@ class MediaThumbnailFormatter extends ImageFormatter {
$link_types = [
'content' => $this->t('Content'),
'media' => $this->t('Media entity'),
'media' => $this->t('Media item'),
];
$element['image_link']['#options'] = $link_types;
@@ -0,0 +1,39 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\media\MediaTypeInterface;
/**
* Media source wrapping around an audio file.
*
* @see \Drupal\file\FileInterface
*
* @MediaSource(
* id = "audio_file",
* label = @Translation("Audio file"),
* description = @Translation("Use audio files for reusable media."),
* allowed_field_types = {"file"},
* default_thumbnail_filename = "audio.png"
* )
*/
class AudioFile extends File {
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
return parent::createSourceField($type)->set('settings', ['file_extensions' => 'mp3 wav aac']);
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'file_audio',
]);
}
}
@@ -22,11 +22,29 @@ use Drupal\media\MediaSourceBase;
*/
class File extends MediaSourceBase {
/**
* Key for "MIME type" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_MIME = 'mimetype';
/**
* Key for "File size" metadata attribute.
*
* @var string
*/
const METADATA_ATTRIBUTE_SIZE = 'filesize';
/**
* {@inheritdoc}
*/
public function getMetadataAttributes() {
return [];
return [
static::METADATA_ATTRIBUTE_MIME => $this->t('MIME type'),
static::METADATA_ATTRIBUTE_SIZE => $this->t('File size'),
];
}
/**
@@ -40,6 +58,12 @@ class File extends MediaSourceBase {
return parent::getMetadata($media, $attribute_name);
}
switch ($attribute_name) {
case 'mimetype':
return $file->getMimeType();
case 'filesize':
return $file->getSize();
case 'default_name':
return $file->getFilename();
@@ -0,0 +1,39 @@
<?php
namespace Drupal\media\Plugin\media\Source;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\media\MediaTypeInterface;
/**
* Media source wrapping around a video file.
*
* @see \Drupal\file\FileInterface
*
* @MediaSource(
* id = "video_file",
* label = @Translation("Video file"),
* description = @Translation("Use video files for reusable media."),
* allowed_field_types = {"file"},
* default_thumbnail_filename = "video.png"
* )
*/
class VideoFile extends File {
/**
* {@inheritdoc}
*/
public function createSourceField(MediaTypeInterface $type) {
return parent::createSourceField($type)->set('settings', ['file_extensions' => 'mp4']);
}
/**
* {@inheritdoc}
*/
public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
$display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
'type' => 'file_video',
]);
}
}
@@ -22,22 +22,6 @@ class Media extends WizardPluginBase {
*/
protected $createdColumn = 'media_field_data-created';
/**
* Set default values for the filters.
*
* @var array
*/
protected $filters = [
'status' => [
'value' => '1',
'table' => 'media_field_data',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'media',
'entity_field' => 'status',
],
];
/**
* {@inheritdoc}
*/
@@ -22,22 +22,6 @@ class MediaRevision extends WizardPluginBase {
*/
protected $createdColumn = 'media_field_revision-created';
/**
* Set default values for the filters.
*
* @var array
*/
protected $filters = [
'status' => [
'value' => '1',
'table' => 'media_field_revision',
'field' => 'status',
'plugin_id' => 'boolean',
'entity_type' => 'media',
'entity_field' => 'status',
],
];
/**
* {@inheritdoc}
*/