diff -u b/core/modules/media/js/media_form.js b/core/modules/media/js/media_form.js --- b/core/modules/media/js/media_form.js +++ b/core/modules/media/js/media_form.js @@ -3,7 +3,7 @@ * Defines Javascript behaviors for the media entity form. */ -(function ($, Drupal, drupalSettings) { +(function ($, Drupal) { 'use strict'; @@ -40 +40 @@ -})(jQuery, Drupal, drupalSettings); +})(jQuery, Drupal); diff -u b/core/modules/media/js/media_type_form.js b/core/modules/media/js/media_type_form.js --- b/core/modules/media/js/media_type_form.js +++ b/core/modules/media/js/media_type_form.js @@ -4,6 +4,7 @@ */ (function ($, Drupal) { + 'use strict'; /** diff -u b/core/modules/media/media.info.yml b/core/modules/media/media.info.yml --- b/core/modules/media/media.info.yml +++ b/core/modules/media/media.info.yml @@ -10 +9,0 @@ - - system diff -u b/core/modules/media/media.links.contextual.yml b/core/modules/media/media.links.contextual.yml --- b/core/modules/media/media.links.contextual.yml +++ b/core/modules/media/media.links.contextual.yml @@ -2,6 +2,7 @@ route_name: entity.media.edit_form group: media title: Edit + entity.media.delete_form: route_name: entity.media.delete_form group: media diff -u b/core/modules/media/media.links.task.yml b/core/modules/media/media.links.task.yml --- b/core/modules/media/media.links.task.yml +++ b/core/modules/media/media.links.task.yml @@ -2,19 +2,23 @@ route_name: entity.media.canonical base_route: entity.media.canonical title: 'View' + entity.media.edit_form: route_name: entity.media.edit_form base_route: entity.media.canonical title: Edit + entity.media.delete_form: route_name: entity.media.delete_form base_route: entity.media.canonical title: Delete weight: 10 + entity.media_type.edit_form: title: 'Edit' route_name: entity.media_type.edit_form base_route: entity.media_type.edit_form + entity.media_type.collection: title: List route_name: entity.media_type.collection diff -u b/core/modules/media/media.module b/core/modules/media/media.module --- b/core/modules/media/media.module +++ b/core/modules/media/media.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Url; /** * Implements hook_help(). @@ -18,13 +19,13 @@ $output .= '

' . t('Uses') . '

'; $output .= '
'; $output .= '
' . t('Creating media items') . '
'; - $output .= '
' . t('When a new media item is created, the Media module records basic information about it, including the author, date of creation, and the Media type. It also manages the publishing options, which define whether or not the item is published. Default settings can be configured for each type of media on your site.', [':media-type' => \Drupal::url('entity.media_type.collection')]) . '
'; + $output .= '
' . t('When a new media item is created, the Media module records basic information about it, including the author, date of creation, and the Media type. It also manages the publishing options, which define whether or not the item is published. Default settings can be configured for each type of media on your site.', [':media-type' => Url::fromRoute('entity.media_type.collection')]) . '
'; $output .= '
' . t('Creating custom media types') . '
'; - $output .= '
' . t('The Media module gives users with the Administer media types permission the ability to create new media types in addition to the default ones already configured. Each media type has an associated media handler (such as the image handler) which support thumbnail generation and metadata extraction. Fields managed by the Field module may be added for storing that metadata, such as width and height, as well as any other associated values.', [':media-new' => \Drupal::url('entity.media_type.add_form'), ':field' => \Drupal::url('help.page', ['name' => 'field'])]) . '
'; + $output .= '
' . t('The Media module gives users with the Administer media types permission the ability to create new media types in addition to the default ones already configured. Each media type has an associated media handler (such as the image handler) which support thumbnail generation and metadata extraction. Fields managed by the Field module may be added for storing that metadata, such as width and height, as well as any other associated values.', [':media-new' => Url::fromRoute('entity.media_type.add_form'), ':field' => Url::fromRoute('help.page', ['name' => 'field'])]) . '
'; $output .= '
' . t('Creating revisions') . '
'; $output .= '
' . t('The Media module also enables you to create multiple versions of any media item, and revert to older versions using the Revision information settings.') . '
'; $output .= '
' . t('User permissions') . '
'; - $output .= '
' . t('The Media module makes a number of permissions available, which can be set by role on the permissions page.', array(':permissions' => \Drupal::url('user.admin_permissions', array(), array('fragment' => 'module-media')))) . '
'; + $output .= '
' . t('The Media module makes a number of permissions available, which can be set by role on the permissions page.', [':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-media'])]) . '
'; $output .= '
'; return $output; } @@ -59,26 +60,26 @@ } /** - * Copy the media file icons to files directory for use with image styles. + * Copies the media file icons to files directory for use with image styles. * * @param string $source * Source folder. * @param string $destination * Destination folder. * - * @throws Exception + * @throws \RuntimeException * Thrown when media icons can't be copied to their destination. */ function media_copy_icons($source, $destination) { if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { - throw new RuntimeException("Unable to create directory $destination."); + throw new \RuntimeException("Unable to create directory $destination."); } $files = file_scan_directory($source, '/.*\.(png|jpg)$/'); foreach ($files as $file) { $result = file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_REPLACE); if (!$result) { - throw new RuntimeException("Unable to copy {$file->uri} to $destination."); + throw new \RuntimeException("Unable to copy {$file->uri} to $destination."); } } } diff -u b/core/modules/media/media.permissions.yml b/core/modules/media/media.permissions.yml --- b/core/modules/media/media.permissions.yml +++ b/core/modules/media/media.permissions.yml @@ -2,18 +2,25 @@ title: 'Administer media' restrict access: TRUE + administer media types: title: 'Administer media types' restrict access: TRUE + view media: title: 'View media' + update media: title: 'Update own media' + update any media: title: 'Update any media' + delete media: title: 'Delete own media' + delete any media: title: 'Delete any media' restrict access: TRUE + create media: title: 'Create media' diff -u b/core/modules/media/media.services.yml b/core/modules/media/media.services.yml --- b/core/modules/media/media.services.yml +++ b/core/modules/media/media.services.yml @@ -2,6 +2,7 @@ plugin.manager.media.handler: class: Drupal\media\MediaHandlerManager parent: default_plugin_manager + media.thumbnail_handler: class: Drupal\media\MediaThumbnailHandler arguments: ['@entity_type.manager'] diff -u b/core/modules/media/media.theme.inc b/core/modules/media/media.theme.inc --- b/core/modules/media/media.theme.inc +++ b/core/modules/media/media.theme.inc @@ -6,7 +6,6 @@ */ use Drupal\Core\Render\Element; -use Drupal\Component\Utility\Html; /** * Prepares variables for media templates. diff -u b/core/modules/media/media.tokens.inc b/core/modules/media/media.tokens.inc --- b/core/modules/media/media.tokens.inc +++ b/core/modules/media/media.tokens.inc @@ -2,13 +2,12 @@ /** * @file - * Builds placeholder replacement tokens for media_entity-related data. + * Builds placeholder replacement tokens for media-related data. */ use Drupal\Core\Datetime\Entity\DateFormat; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Render\BubbleableMetadata; -use Drupal\media\Entity\MediaBundle; /** * Implements hook_token_info(). @@ -139,7 +138,6 @@ // Default values for the chained tokens handled below. case 'author': - /** @var \Drupal\user\UserInterface $account */ $account = $media->getOwner(); $bubbleable_metadata->addCacheableDependency($account); $replacements[$original] = $account->label(); diff -u b/core/modules/media/src/Annotation/MediaHandler.php b/core/modules/media/src/Annotation/MediaHandler.php --- b/core/modules/media/src/Annotation/MediaHandler.php +++ b/core/modules/media/src/Annotation/MediaHandler.php @@ -7,7 +7,19 @@ /** * Defines a media handler plugin annotation object. * + * Media handlers are responsible for implementing all the logic for dealing + * with a particular type of media asset. They provide various universal and + * type-specific metadata about media assets of the type they handle. + * + * Plugin namespace: Plugin\media\Handler + * + * For a working example, see \Drupal\media\Plugin\media\Handler\File. + * + * @see \Drupal\media\MediaHandlerInterface + * @see \Drupal\media\MediaHandlerBase + * @see \Drupal\media\MediaHandlerManager * @see hook_media_handler_info_alter() + * @see plugin_api * * @Annotation */ diff -u b/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php --- b/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -30,9 +30,9 @@ * "list_builder" = "Drupal\Core\Entity\EntityListBuilder", * "access" = "Drupal\media\MediaAccessController", * "form" = { - * "default" = "Drupal\media\MediaForm", + * "add" = "Drupal\media\MediaForm", + * "edit" = "Drupal\media\MediaForm", * "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm", - * "edit" = "Drupal\media\MediaForm" * }, * "translation" = "Drupal\content_translation\ContentTranslationHandler", * "views_data" = "Drupal\media\MediaViewsData", @@ -303,7 +303,6 @@ ), )); - return $fields; } @@ -312,7 +311,7 @@ * * @see ::baseFieldDefinitions() * - * @return array + * @return int[] * An array of default values. */ public static function getCurrentUserId() { diff -u b/core/modules/media/src/Entity/MediaType.php b/core/modules/media/src/Entity/MediaType.php --- b/core/modules/media/src/Entity/MediaType.php +++ b/core/modules/media/src/Entity/MediaType.php @@ -48,7 +48,6 @@ * "handler", * "queue_thumbnail_downloads", * "new_revision", - * "third_party_settings", * "handler_configuration", * "field_map", * "status", @@ -58,7 +57,7 @@ * "edit-form" = "/admin/structure/media/manage/{media_type}", * "delete-form" = "/admin/structure/media/manage/{media_type}/delete", * "collection" = "/admin/structure/media", - * } + * }, * ) */ class MediaType extends ConfigEntityBundleBase implements MediaTypeInterface, EntityWithPluginCollectionInterface { @@ -85,14 +84,14 @@ protected $description; /** - * The handler plugin id. + * The handler plugin ID. * * @var string */ protected $handler; /** - * Are thumbnail downloads queued. + * Whether thumbnail downloads are queued. * * @var bool */ @@ -113,7 +112,7 @@ protected $handler_configuration = []; /** - * Lazy collection of handler plugin(s). + * Lazy collection for the handler plugin. * * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection */ @@ -171,6 +170,7 @@ public function setHandlerConfiguration(array $configuration) { $this->handler_configuration = $configuration; $this->handlerPluginCollection = NULL; + return $this; } /** @@ -185,6 +185,7 @@ */ public function setQueueThumbnailDownloads($queue_thumbnail_downloads) { $this->queue_thumbnail_downloads = $queue_thumbnail_downloads; + return $this; } /** @@ -226,6 +227,7 @@ */ public function setNewRevision($new_revision) { $this->new_revision = $new_revision; + return $this; } /** diff -u b/core/modules/media/src/Form/DeleteMultiple.php b/core/modules/media/src/Form/DeleteMultiple.php --- b/core/modules/media/src/Form/DeleteMultiple.php +++ b/core/modules/media/src/Form/DeleteMultiple.php @@ -134,9 +134,7 @@ '#theme' => 'item_list', '#items' => $items, ]; - $form = parent::buildForm($form, $form_state); - - return $form; + return parent::buildForm($form, $form_state); } /** diff -u b/core/modules/media/src/MediaAccessController.php b/core/modules/media/src/MediaAccessController.php --- b/core/modules/media/src/MediaAccessController.php +++ b/core/modules/media/src/MediaAccessController.php @@ -20,7 +20,7 @@ return AccessResult::allowed()->cachePerPermissions(); } - $is_owner = ($account->id() && $account->id() == $entity->getOwnerId()) ? TRUE : FALSE; + $is_owner = ($account->id() && $account->id() == $entity->getOwnerId()); switch ($operation) { case 'view': return AccessResult::allowedIf($account->hasPermission('view media') && $entity->isPublished())->cachePerPermissions()->addCacheableDependency($entity); diff -u b/core/modules/media/src/MediaForm.php b/core/modules/media/src/MediaForm.php --- b/core/modules/media/src/MediaForm.php +++ b/core/modules/media/src/MediaForm.php @@ -4,6 +4,7 @@ use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Form\FormStateInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for the media edit forms. @@ -33,7 +34,7 @@ // Set up default values, if required. if (!$this->getEntity()->isNew()) { $this->entity->setOwnerId($this->currentUser()->id()); - $this->entity->setCreatedTime(\Drupal::time()->getRequestTime()); + $this->entity->setCreatedTime($this->time->getRequestTime()); } } @@ -151,13 +152,11 @@ */ public function updateStatus($entity_type_id, MediaInterface $media, array $form, FormStateInterface $form_state) { $element = $form_state->getTriggeringElement(); - if (isset($element['#published_status'])) { - if ((bool) $element['#published_status']) { - $media->setPublished(); - } - else { - $media->setUnpublished(); - } + if (!empty($element['#published_status'])) { + $media->setPublished(); + } + else { + $media->setUnpublished(); } } @@ -165,13 +164,12 @@ * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - $insert = $this->entity->isNew(); - $this->entity->save(); + $saved = parent::save($form, $form_state); $context = ['@type' => $this->entity->bundle(), '%label' => $this->entity->label()]; $logger = $this->logger('media'); $t_args = ['@type' => $this->entity->bundle->entity->label(), '%label' => $this->entity->label()]; - if ($insert) { + if ($saved === SAVED_NEW) { $logger->notice('@type: added %label.', $context); drupal_set_message($this->t('@type %label has been created.', $t_args)); } @@ -181,6 +179,7 @@ } $form_state->setRedirectUrl($this->entity->toUrl('canonical')); + return $saved; } } diff -u b/core/modules/media/src/MediaHandlerBase.php b/core/modules/media/src/MediaHandlerBase.php --- b/core/modules/media/src/MediaHandlerBase.php +++ b/core/modules/media/src/MediaHandlerBase.php @@ -26,14 +26,14 @@ /** * The entity type manager service. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface; + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The entity field manager service. * - * @var \Drupal\Core\Entity\EntityFieldManagerInterface; + * @var \Drupal\Core\Entity\EntityFieldManagerInterface */ protected $entityFieldManager; @@ -118,20 +118,25 @@ /** * {@inheritdoc} */ - public function label() { + public function getLabel() { return $this->label; } /** * {@inheritdoc} */ - public function attachConstraints(MediaInterface $media) {} + public function attachConstraints(MediaInterface $media) { + } /** * {@inheritdoc} */ public function calculateDependencies() { - return []; + $dependencies = []; + if ($this->configuration['source_field']) { + $dependencies['config'][] = 'field.storage.' . $this->configuration['source_field']; + } + return $dependencies; } /** @@ -151,10 +156,9 @@ if ($options) { $form['source_field'] = [ '#type' => 'select', - '#title' => $this->t('Field with source information.'), + '#title' => $this->t('Field with source information'), '#default_value' => $this->configuration['source_field'], '#empty_option' => $this->t('- Create -'), - '#empty_value' => NULL, '#options' => $options, '#description' => $this->t('The field on media items of this type that will store the source information.'), ]; @@ -165,12 +169,14 @@ /** * {@inheritdoc} */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {} + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + } /** * {@inheritdoc} */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {} + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + } /** * {@inheritdoc} @@ -182,7 +188,7 @@ /** * {@inheritdoc} */ - public function getSourceField(MediaTypeInterface $type) { + public function getSourceField(MediaTypeInterface $type, $retry = TRUE) { // If we don't know the name of the source field, we definitely need to // create it. if (empty($this->configuration['source_field'])) { @@ -232,7 +238,14 @@ * The unsaved field definition. The field storage definition, if new, * should also be unsaved. */ - abstract protected function createSourceField(MediaTypeInterface $type); + protected function createSourceField(MediaTypeInterface $type) { + return $this->entityTypeManager + ->getStorage('field_config') + ->create([ + 'field_storage' => $this->getSourceFieldStorage(), + 'bundle' => $type->id(), + ]); + } /** * Determine the name of the source field. @@ -259,8 +272,7 @@ } $field = $storage->load('media.' . $id); $tries++; - } - while ($field); + } while ($field); return $id; } diff -u b/core/modules/media/src/MediaHandlerInterface.php b/core/modules/media/src/MediaHandlerInterface.php --- b/core/modules/media/src/MediaHandlerInterface.php +++ b/core/modules/media/src/MediaHandlerInterface.php @@ -7,7 +7,12 @@ use Drupal\Core\Plugin\PluginFormInterface; /** - * Defines the interface for media handler. + * Defines the interface for media handler plugins. + * + * @see \Drupal\media\Annotation\MediaHandler + * @see \Drupal\media\MediaHandlerBase + * @see \Drupal\media\MediaHandlerManager + * @see plugin_api */ interface MediaHandlerInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { @@ -17,7 +22,7 @@ * @return string * The display label of the media handler plugin. */ - public function label(); + public function getLabel(); /** * Gets list of fields provided by this plugin. @@ -29,13 +34,13 @@ * - field_type: (optional) entity field type that the field should be * mapped to by default. "string" will be assumed if omitted. */ - public function providedFields(); + public function getProvidedFields(); /** * Gets a media-related field/value. * - * @param MediaInterface $media - * Media object. + * @param \Drupal\media\MediaInterface $media + * The media entity. * @param string $name * Name of field to fetch. * @@ -55,8 +60,9 @@ /** * Gets thumbnail image. * - * Media handler plugin is responsible for returning URI of the generic thumbnail - * if no other is available. This function should always return a valid URI. + * Media handler plugin is responsible for returning URI of the generic + * thumbnail if no other is available. This function should always return a + * valid URI. * * @param \Drupal\media\MediaInterface $media * The media entity. @@ -64,7 +70,7 @@ * @return string * URI of the thumbnail. */ - public function thumbnail(MediaInterface $media); + public function getThumbnail(MediaInterface $media); /** * Gets the default thumbnail image. @@ -95,9 +101,10 @@ * @param \Drupal\media\MediaInterface $media - * Media entity object. + * The media entity. * @param $source_field * Name of the source metadata field. * @param $destination_field * Name of the destination entity field. */ public function mapFieldValue(MediaInterface $media, $source_field, $destination_field); + } diff -u b/core/modules/media/src/MediaHandlerManager.php b/core/modules/media/src/MediaHandlerManager.php --- b/core/modules/media/src/MediaHandlerManager.php +++ b/core/modules/media/src/MediaHandlerManager.php @@ -5,6 +5,7 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\media\Annotation\MediaHandler; /** * Manages media handler plugins. @@ -23,7 +24,7 @@ * The module handler. */ public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { - parent::__construct('Plugin/media/Handler', $namespaces, $module_handler, 'Drupal\media\MediaHandlerInterface', 'Drupal\media\Annotation\MediaHandler'); + parent::__construct('Plugin/media/Handler', $namespaces, $module_handler, MediaHandlerInterface::class, MediaHandler::class); $this->alterInfo('media_handler_info'); $this->setCacheBackend($cache_backend, 'media_handler_plugins'); diff -u b/core/modules/media/src/MediaThumbnailHandler.php b/core/modules/media/src/MediaThumbnailHandler.php --- b/core/modules/media/src/MediaThumbnailHandler.php +++ b/core/modules/media/src/MediaThumbnailHandler.php @@ -5,16 +5,16 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; /** - * Provides an interface defining a media thumbnail service. + * Provides a service for handling media thumbnails. */ class MediaThumbnailHandler implements MediaThumbnailHandlerInterface { /** - * The entity type manager service. + * The file entity storage handler. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * @var \Drupal\file\FileStorageInterface */ - protected $entityTypeManager; + protected $fileStorage; /** * Constructs a new MediaThumbnailHandler. @@ -23,25 +23,22 @@ * The entity type manager service. */ public function __construct(EntityTypeManagerInterface $entity_type_manager) { - $this->entityTypeManager = $entity_type_manager; + $this->fileStorage = $entity_type_manager->getStorage('file'); } /** * {@inheritdoc} */ public function setThumbnail(MediaInterface $media) { - /** @var \Drupal\Core\Entity\EntityStorageInterface $file_storage */ - $file_storage = $this->entityTypeManager->getStorage('file'); - // If thumbnail fetching should be queued then temporary use default // thumbnail or fetch it immediately otherwise. if ($media->bundle->entity->getQueueThumbnailDownloads() && $media->isNew()) { $thumbnail_uri = $media->getHandler()->getDefaultThumbnail(); } else { - $thumbnail_uri = $media->getHandler()->thumbnail($media); + $thumbnail_uri = $media->getHandler()->getThumbnail($media); } - $existing = $file_storage->getQuery() + $existing = $this->fileStorage->getQuery() ->condition('uri', $thumbnail_uri) ->execute(); @@ -50,7 +47,7 @@ } else { /** @var \Drupal\file\FileInterface $file */ - $file = $file_storage->create(['uri' => $thumbnail_uri]); + $file = $this->fileStorage->create(['uri' => $thumbnail_uri]); if ($owner = $media->getOwner()) { $file->setOwner($owner); } diff -u b/core/modules/media/src/MediaTypeForm.php b/core/modules/media/src/MediaTypeForm.php --- b/core/modules/media/src/MediaTypeForm.php +++ b/core/modules/media/src/MediaTypeForm.php @@ -72,14 +72,6 @@ /** * Ajax callback triggered by the type provider select element. - * - * @param array $form - * The form array. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * Current form state. - * - * @return \Drupal\Core\Ajax\AjaxResponse - * The ajax response. */ public function ajaxHandlerData(array $form, FormStateInterface $form_state) { $response = new AjaxResponse(); @@ -93,8 +85,7 @@ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - /** @var \Drupal\media\MediaTypeInterface $bundle */ - $form['#entity'] = $bundle = $this->getEntity(); + /* $form['#entity'] = */$bundle = $this->entity; $form_state->set('bundle', $bundle->id()); /** @var \Drupal\media\MediaHandlerInterface $handler */ @@ -108,8 +99,6 @@ if ($this->operation == 'add') { $form['#title'] = $this->t('Add media type'); } - elseif ($this->operation == 'edit') { - } $form['label'] = [ '#title' => $this->t('Name'), @@ -128,7 +117,6 @@ '#disabled' => !$bundle->isNew(), '#machine_name' => [ 'exists' => [MediaType::class, 'load'], - 'source' => ['label'], ], '#description' => $this->t('A unique machine-readable name for this media type.'), '#weight' => -90, @@ -144,8 +132,8 @@ $plugins = $this->handlerManager->getDefinitions(); $options = []; - foreach ($plugins as $plugin => $definition) { - $options[$plugin] = $definition['label']; + foreach ($plugins as $plugin_id => $definition) { + $options[$plugin_id] = $definition['label']; } $form['handler_dependent'] = [ @@ -191,14 +179,12 @@ '#title' => $this->t('Field mapping'), '#tree' => TRUE, 'description' => [ - '#type' => 'html_tag', - '#tag' => 'p', - '#value' => $this->t('Media handlers can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.'), + '#markup' => '

' . $this->t('Media handlers can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.') . '

', ], '#weight' => -50, ]; - if (empty($handler) || empty($handler->providedFields())) { + if (empty($handler) || empty($handler->getProvidedFields())) { $form['handler_dependent']['field_mapping']['#access'] = FALSE; } else { @@ -210,7 +196,7 @@ } $field_map = $bundle->getFieldMap(); - foreach ($handler->providedFields() as $field_name => $field_definition) { + foreach ($handler->getProvidedFields() as $field_name => $field_definition) { // This is a BC layer. In the past this function returned string and now // it returns arrays. if (!is_array($field_definition)) { @@ -364,9 +350,8 @@ * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - /** @var \Drupal\media\MediaTypeInterface $bundle */ + $status = parent::save($form, $form_state); $bundle = $this->entity; - $status = $bundle->save(); $t_args = ['%name' => $bundle->label()]; if ($status == SAVED_UPDATED) { diff -u b/core/modules/media/src/MediaTypeInterface.php b/core/modules/media/src/MediaTypeInterface.php --- b/core/modules/media/src/MediaTypeInterface.php +++ b/core/modules/media/src/MediaTypeInterface.php @@ -15,7 +15,7 @@ * Returns the label. * * @param \Drupal\media\MediaInterface $media - * The Media entity. + * The media entity. * * @return string|bool * Returns the label of the bundle that entity belongs to. @@ -26,7 +26,8 @@ * Returns whether thumbnail downloads are queued. * * @return bool - * Returns download now or later. + * TRUE if thumbnails are queued for download later, FALSE if they should be + * downloaded now. */ public function getQueueThumbnailDownloads(); @@ -35,6 +36,8 @@ * * @param bool $queue_thumbnail_downloads * The queue downloads flag. + * + * @return $this */ public function setQueueThumbnailDownloads($queue_thumbnail_downloads); @@ -59,22 +62,18 @@ * * @param array $configuration * The handler configuration. - */ - public function setHandlerConfiguration(array $configuration); - - /** - * Returns the media type status. * - * @return bool - * The status. + * @return $this */ - public function getStatus(); + public function setHandlerConfiguration(array $configuration); /** - * Sets whether a new revision should be created by default. + * Sets whether new revisions should be created by default. * * @param bool $new_revision - * TRUE if a new revision should be created by default. + * TRUE if media items of this type should create new revisions by default. + * + * @return $this */ public function setNewRevision($new_revision); diff -u b/core/modules/media/src/Plugin/Action/DeleteMedia.php b/core/modules/media/src/Plugin/Action/DeleteMedia.php --- b/core/modules/media/src/Plugin/Action/DeleteMedia.php +++ b/core/modules/media/src/Plugin/Action/DeleteMedia.php @@ -15,7 +15,7 @@ * id = "media_delete_action", * label = @Translation("Delete media"), * type = "media", - * confirm_form_route_name = "entity.media.multiple_delete_confirm" + * confirm_form_route_name = "entity.media.multiple_delete_confirm", * ) */ class DeleteMedia extends ActionBase implements ContainerFactoryPluginInterface { diff -u b/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php b/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php --- b/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php +++ b/core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php @@ -5,6 +5,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\image\ImageStyleStorageInterface; use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Render\RendererInterface; @@ -51,10 +52,12 @@ * Any third party settings settings. * @param \Drupal\Core\Session\AccountInterface $current_user * The current user. + * @param \Drupal\image\ImageStyleStorageInterface $image_style_storage + * The image style entity storage handler. * @param \Drupal\Core\Render\RendererInterface $renderer * The renderer service. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityStorageInterface $image_style_storage, RendererInterface $renderer) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, ImageStyleStorageInterface $image_style_storage, RendererInterface $renderer) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $current_user, $image_style_storage); $this->renderer = $renderer; } @@ -72,7 +75,7 @@ $configuration['view_mode'], $configuration['third_party_settings'], $container->get('current_user'), - $container->get('entity.manager')->getStorage('image_style'), + $container->get('entity_type.manager')->getStorage('image_style'), $container->get('renderer') ); } @@ -80,7 +83,7 @@ /** * {@inheritdoc} * - * This has to be overriden because FileFormatterBase expects $item to be + * This has to be overridden because FileFormatterBase expects $item to be * of type \Drupal\file\Plugin\Field\FieldType\FileItem and calls * isDisplayed() which is not in FieldItemInterface. */ @@ -149,7 +152,7 @@ $image_style_setting = $this->getSetting('image_style'); - /** @var \Drupal\media\MediaInterface $media_item */ + /** @var \Drupal\media\MediaInterface[] $media */ foreach ($media as $delta => $media_item) { if (isset($link_media)) { $url = $media_item->toUrl(); diff -u b/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php b/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php --- b/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php +++ b/core/modules/media/tests/modules/media_test_handler/src/Plugin/media/Handler/Test.php @@ -5,7 +5,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\media\MediaInterface; use Drupal\media\MediaHandlerBase; -use Drupal\media\MediaTypeInterface; /** * Provides generic media type. @@ -22,14 +21,14 @@ /** * {@inheritdoc} */ - public function thumbnail(MediaInterface $media) { + public function getThumbnail(MediaInterface $media) { return $this->getDefaultThumbnail(); } /** * {@inheritdoc} */ - public function providedFields() { + public function getProvidedFields() { return [ 'field_1' => $this->t('Field 1'), 'field_2' => $this->t('Field 2'), @@ -84,15 +83,2 @@ - /** - * {@inheritdoc} - */ - protected function createSourceField(MediaTypeInterface $type) { - /** @var \Drupal\field\FieldConfigInterface $field */ - return $this->entityTypeManager - ->getStorage('field_config') - ->create([ - 'field_storage' => $this->getSourceFieldStorage(), - 'bundle' => $type->id(), - ]); - } - } diff -u b/core/modules/media/tests/src/Functional/MediaAccessTest.php b/core/modules/media/tests/src/Functional/MediaAccessTest.php --- b/core/modules/media/tests/src/Functional/MediaAccessTest.php +++ b/core/modules/media/tests/src/Functional/MediaAccessTest.php @@ -24,7 +24,7 @@ */ protected function setUp() { parent::setUp(); - $this->testBundle = $this->drupalCreateMediaBundle(); + $this->testBundle = $this->drupalCreateMediaType(); } /** diff -u b/core/modules/media/tests/src/Functional/MediaBulkFormTest.php b/core/modules/media/tests/src/Functional/MediaBulkFormTest.php --- b/core/modules/media/tests/src/Functional/MediaBulkFormTest.php +++ b/core/modules/media/tests/src/Functional/MediaBulkFormTest.php @@ -39,7 +39,7 @@ protected function setUp() { parent::setUp(); - $this->testBundle = $this->drupalCreateMediaBundle(); + $this->testBundle = $this->drupalCreateMediaType(); // Create some test media entities. $this->mediaEntities = []; diff -u b/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php b/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php --- b/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php +++ b/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php @@ -11,7 +11,9 @@ */ abstract class MediaFunctionalTestBase extends BrowserTestBase { - use MediaFunctionalTestTrait; + use MediaFunctionalTestTrait { + createMediaType as drupalCreateMediaType; + } /** * Modules to enable. diff -u b/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php b/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php --- b/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php +++ b/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php @@ -5,25 +5,23 @@ use Drupal\media\Entity\MediaType; /** - * Trait with helpers for Media Entity functional tests. - * - * @package Drupal\Tests\media\Functional + * Trait with helpers for Media functional tests. */ trait MediaFunctionalTestTrait { /** - * Creates a media bundle. + * Creates a media type. * * @param array $values - * The media bundle values. - * @param string $type_name - * (optional) The media type provider plugin that is responsible for - * additional logic related to this media). + * The media type values. + * @param string $handler + * (optional) The handler plugin that is responsible for additional logic + * related to this media type. * - * @return \Drupal\Core\Entity\EntityInterface - * Returns newly created media bundle. + * @return \Drupal\media\MediaTypeInterface + * A newly created media type. */ - protected function drupalCreateMediaBundle(array $values = [], $type_name = 'test') { + protected function createMediaType(array $values = [], $handler = 'test') { if (!isset($values['bundle'])) { $id = strtolower($this->randomMachineName()); } @@ -33,7 +31,7 @@ $values += [ 'id' => $id, 'label' => $id, - 'handler' => $type_name, + 'handler' => $handler, 'handler_configuration' => [], 'field_map' => [], 'new_revision' => FALSE, diff -u b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php --- b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php +++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php @@ -45,8 +45,7 @@ $page = $session->getPage(); $assert_session = $this->assertSession(); - /** @var \Drupal\media\MediaTypeInterface $bundle */ - $bundle = $this->drupalCreateMediaBundle(['new_revision' => FALSE]); + $bundle = $this->drupalCreateMediaType(['new_revision' => FALSE]); $this->drupalGet('media/add'); $assert_session->statusCodeEquals(200); @@ -130,10 +129,10 @@ $assert_session = $this->assertSession(); // Tests and creates the first media bundle. - $first_media_bundle = $this->drupalCreateMediaBundle(['description' => $this->randomMachineName(32)]); + $first_media_bundle = $this->drupalCreateMediaType(['description' => $this->randomMachineName(32)]); // Test and create a second media bundle. - $second_media_bundle = $this->drupalCreateMediaBundle(['description' => $this->randomMachineName(32)]); + $second_media_bundle = $this->drupalCreateMediaType(['description' => $this->randomMachineName(32)]); // Test if media/add displays two media bundle options. $this->drupalGet('media/add'); diff -u b/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php b/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php --- b/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaJavascriptTestBase.php @@ -12,7 +12,9 @@ */ abstract class MediaJavascriptTestBase extends JavascriptTestBase { - use MediaFunctionalTestTrait; + use MediaFunctionalTestTrait { + createMediaType as drupalCreateMediaType; + } /** * Modules to enable. diff -u b/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php --- b/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaUiJavascriptTest.php @@ -165,7 +165,7 @@ $assert_session->pageTextContains('The media type ' . $new_name . ' has been deleted.'); // Test bundle delete prevention when there is existing media. - $bundle2 = $this->drupalCreateMediaBundle(); + $bundle2 = $this->drupalCreateMediaType(); $label2 = $bundle2->label(); $media = Media::create(['name' => 'lorem ipsum', 'bundle' => $bundle2->id()]); $media->save(); diff -u b/core/modules/media/tests/src/Kernel/BasicCreationTest.php b/core/modules/media/tests/src/Kernel/BasicCreationTest.php --- b/core/modules/media/tests/src/Kernel/BasicCreationTest.php +++ b/core/modules/media/tests/src/Kernel/BasicCreationTest.php @@ -6,6 +6,8 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\media\Entity\Media; use Drupal\media\Entity\MediaType; +use Drupal\media\MediaInterface; +use Drupal\media\MediaTypeInterface; /** * Tests creation of Media Bundles and Media Entities. @@ -65,13 +67,12 @@ public function testMediaBundleCreation() { $bundle_storage = $this->container->get('entity_type.manager')->getStorage('media_type'); - $bundle_exists = (bool) $bundle_storage->load($this->testBundle->id()); - $this->assertTrue($bundle_exists, 'The new media type has not been correctly created in the database.'); + $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testBundle->id()), 'The new media type has not been correctly created in the database.'); // Test a media type created from default configuration. $this->container->get('module_installer')->install(['media_test_type']); $test_bundle = $bundle_storage->load('test'); - $this->assertTrue((bool) $test_bundle, 'The media type from default configuration has not been created in the database.'); + $this->assertInstanceOf(MediaTypeInterface::class, $test_bundle, 'The media type from default configuration has not been created in the database.'); $this->assertEquals('Test type', $test_bundle->get('label'), 'Could not assure the correct type name.'); $this->assertEquals('Test type.', $test_bundle->get('description'), 'Could not assure the correct type description.'); $this->assertEquals('test', $test_bundle->get('handler'), 'Could not assure the correct handler.'); @@ -89,11 +90,9 @@ ]); $media->save(); - $media_not_exist = (bool) Media::load(rand(1000, 9999)); - $this->assertFalse($media_not_exist, 'Failed asserting a non-existent media.'); + $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.'); - $media_exists = (bool) Media::load($media->id()); - $this->assertTrue($media_exists, 'The new media entity has not been created in the database.'); + $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media entity has not been created in the database.'); $this->assertEquals($this->testBundle->id(), $media->bundle(), 'The media was not created with the correct type.'); $this->assertEquals('Unnamed', $media->label(), 'The media was not created with the correct name.'); diff -u b/core/modules/media/tests/src/Kernel/TokensTest.php b/core/modules/media/tests/src/Kernel/TokensTest.php --- b/core/modules/media/tests/src/Kernel/TokensTest.php +++ b/core/modules/media/tests/src/Kernel/TokensTest.php @@ -15,9 +15,7 @@ class TokensTest extends EntityKernelTestBase { /** - * Modules to install. - * - * @var array + * {@inheritdoc} */ public static $modules = [ 'media', @@ -70,7 +68,7 @@ $token_service = $this->container->get('token'); $replaced_value = $token_service->replace('[media:name]', ['media' => $media]); - $this->assertEquals($media->label(), $replaced_value, 'Token replacement for the media label was sucessful.'); + $this->assertEquals($media->label(), $replaced_value, 'Token replacement for the media label was successful.'); } }