diff -u b/core/modules/media/config/schema/media.schema.yml b/core/modules/media/config/schema/media.schema.yml --- b/core/modules/media/config/schema/media.schema.yml +++ b/core/modules/media/config/schema/media.schema.yml @@ -58,7 +58,7 @@ media.handler.*: type: mapping - label: 'Media handler plugin settings' + label: 'Media handler settings' media.handler.field_aware: type: mapping 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 @@ -1,6 +1,6 @@ /** * @file - * Defines Javascript behaviors for the media entity form. + * Defines Javascript behaviors for the media form. */ (function ($, Drupal) { diff -u b/core/modules/media/media.api.php b/core/modules/media/media.api.php --- b/core/modules/media/media.api.php +++ b/core/modules/media/media.api.php @@ -14,7 +14,7 @@ * Alter the information provided in \Drupal\media\Annotation\MediaHandler. * * @param array $handlers - * The array of handler plugin definitions, keyed by plugin ID. + * The array of media handler plugin definitions, keyed by plugin ID. */ function hook_media_handler_info_alter(array &$handlers) { $handlers['youtube']['label'] = t('Youtube rocks!'); diff -u b/core/modules/media/media.install b/core/modules/media/media.install --- b/core/modules/media/media.install +++ b/core/modules/media/media.install @@ -15,7 +15,7 @@ throw new \RuntimeException("Unable to create directory $destination."); } - $files = file_scan_directory($source, '/.*\.(png|jpg)$/'); + $files = file_scan_directory($source, '/.*\.(svg|png|jpg|jpeg|gif)$/'); foreach ($files as $file) { $result = file_unmanaged_copy($file->uri, $destination, FILE_EXISTS_REPLACE); if (!$result) { 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 @@ -2,7 +2,7 @@ /** * @file - * Provides media entities. + * Provides media items. */ use Drupal\Core\Access\AccessResult; 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 @@ -30,7 +30,7 @@ ]; $media['vid'] = [ 'name' => t('Revision ID'), - 'description' => t("'The unique ID of the media's latest revision."), + 'description' => t("'The unique ID of the media item's latest revision."), ]; $media['bundle'] = [ 'name' => t('Media type'), @@ -41,11 +41,11 @@ ]; $media['langcode'] = [ 'name' => t('Language code'), - 'description' => t('The language code of the language the media is written in.'), + 'description' => t('The language code of the language the media item is written in.'), ]; $media['name'] = [ 'name' => t('Name'), - 'description' => t('The name of this media.'), + 'description' => t('The name of this media item.'), ]; $media['author'] = [ 'name' => t('Author'), @@ -53,11 +53,11 @@ ]; $media['url'] = [ 'name' => t('URL'), - 'description' => t('The URL of the media.'), + 'description' => t('The URL of the media item.'), ]; $media['edit-url'] = [ 'name' => t('Edit URL'), - 'description' => t("The URL of the media's edit page."), + 'description' => t("The URL of the media item's edit page."), ]; // Chained tokens for media. @@ -67,7 +67,7 @@ ]; $media['changed'] = [ 'name' => t('Date changed'), - 'description' => t('The date the media was most recently updated.'), + 'description' => t('The date the media item was most recently updated.'), 'type' => 'date', ]; 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 @@ -33,7 +33,7 @@ public $id; /** - * The human-readable name of the handler. + * The human-readable name of the media handler. * * @var \Drupal\Core\Annotation\Translation * @@ -42,7 +42,7 @@ public $label; /** - * A brief description of the handler. + * A brief description of the media handler. * * @var \Drupal\Core\Annotation\Translation * @@ -51,7 +51,7 @@ public $description = ''; /** - * The field types that can be used as a source field for this handler. + * The field types that can be used as a source field for this media handler. * * @var string[] */ 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 @@ -14,8 +14,8 @@ /** * Defines the media entity class. * - * @todo Remove "default" form handler when https://www.drupal.org/node/2006348 - * lands. + * @todo Remove default/fallback entity form operation when #2006348 lands. + * @see https://www.drupal.org/node/2006348. * * @ContentEntityType( * id = "media", @@ -137,12 +137,13 @@ public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); - // Try to set fields provided by the handler and mapped in bundle config. + // Try to set fields provided by the media handler and mapped in + // media type config. foreach ($this->bundle->entity->getFieldMap() as $source_field => $destination_field) { $this->getHandler()->mapFieldValue($this, $source_field, $destination_field); } - // Try to set a default name for this media if no label is provided. + // Try to set a default name for this media item if no label is provided. if (!$this->label()) { $this->set('name', $this->getHandler()->getDefaultName($this)); } @@ -220,7 +221,7 @@ $fields['thumbnail'] = BaseFieldDefinition::create('image') ->setLabel(t('Thumbnail')) - ->setDescription(t('The thumbnail of the media.')) + ->setDescription(t('The thumbnail of the media item.')) ->setRevisionable(TRUE) ->setDisplayOptions('view', [ 'type' => 'image', 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 @@ -8,7 +8,6 @@ use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection; use Drupal\field\FieldStorageConfigInterface; use Drupal\media\MediaTypeInterface; -use Drupal\media\MediaInterface; use Drupal\media\SourceFieldInterface; /** @@ -85,7 +84,7 @@ protected $description; /** - * The handler plugin ID. + * The media handler ID. * * @var string */ @@ -113,14 +112,14 @@ protected $new_revision = FALSE; /** - * The handler plugin configuration. + * The media handler configuration. * * @var array */ protected $handler_configuration = []; /** - * Lazy collection for the handler plugin. + * Lazy collection for the media handler. * * @var \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection */ @@ -154,14 +153,6 @@ /** * {@inheritdoc} */ - public static function getLabel(MediaInterface $media) { - $bundle = static::load($media->bundle()); - return $bundle ? $bundle->label() : FALSE; - } - - /** - * {@inheritdoc} - */ public function getDescription() { return $this->description; } @@ -197,7 +188,7 @@ } /** - * Returns handler lazy plugin collection. + * Returns media handler lazy plugin collection. * * @return \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection * The tag plugin collection. @@ -235,8 +226,9 @@ * {@inheritdoc} */ public function preSave(EntityStorageInterface $storage) { - // If the handler uses a source field, we'll need to store its name before - // saving. We'd need to double-save if we did this in postSave(). + // If the media handler uses a source field, we'll need to store + // its name before saving. We'd need to double-save if we did + // this in postSave(). $handler = $this->getHandler(); if ($this->auto_create_source_field && $handler instanceof SourceFieldInterface) { $field_storage = $handler->getSourceField($this)->getFieldStorageDefinition(); @@ -255,28 +247,28 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) { parent::postSave($storage, $update); - // If the handler is using a source field, we may need to save it if it's - // new. The field storage is guaranteed to exist already because preSave() - // took care of that. + // If the media handler is using a source field, we may need to save + // it if it's new. The field storage is guaranteed to exist already + // because preSave() took care of that. $handler = $this->getHandler(); if ($this->auto_create_source_field && $handler instanceof SourceFieldInterface) { $field = $handler->getSourceField($this); - // If the field is new, save it and add it to this bundle's view and form - // displays. + // If the field is new, save it and add it to this media type's view + // and form displays. if ($field->isNew()) { // Ensure the field is saved correctly before adding it to the displays. $field->save(); $entity_type = $field->getTargetEntityTypeId(); - $bundle = $field->getTargetBundle(); + $media_type = $field->getTargetBundle(); if ($field->isDisplayConfigurable('form')) { // Use the default widget and settings. $component = \Drupal::service('plugin.manager.field.widget') ->prepareConfiguration($field->getType(), []); - entity_get_form_display($entity_type, $bundle, 'default') + entity_get_form_display($entity_type, $media_type, 'default') ->setComponent($field->getName(), $component) ->save(); } @@ -285,7 +277,7 @@ $component = \Drupal::service('plugin.manager.field.formatter') ->prepareConfiguration($field->getType(), []); - entity_get_display($entity_type, $bundle, 'default') + entity_get_display($entity_type, $media_type, 'default') ->setComponent($field->getName(), $component) ->save(); } diff -u b/core/modules/media/src/Form/MediaDeleteMultipleConfirmForm.php b/core/modules/media/src/Form/MediaDeleteMultipleConfirmForm.php --- b/core/modules/media/src/Form/MediaDeleteMultipleConfirmForm.php +++ b/core/modules/media/src/Form/MediaDeleteMultipleConfirmForm.php @@ -77,6 +77,8 @@ * {@inheritdoc} */ public function getCancelUrl() { + // @todo Change to media library when #2834729 is done. + // https://www.drupal.org/node/2834729. return new Url('system.admin_content'); } @@ -89,6 +91,9 @@ /** * {@inheritdoc} + * + * @todo: Change to trait or base class when #2843395 is done. + * @see https://www.drupal.org/node/2843395 */ public function buildForm(array $form, FormStateInterface $form_state) { $this->mediaItems = $this->tempStoreFactory->get('media_multiple_delete_confirm')->get($this->currentUser()->id()); @@ -139,6 +144,9 @@ /** * {@inheritdoc} + * + * @todo: Change to trait or base class when #2843395 is done. + * @see https://www.drupal.org/node/2843395 */ public function submitForm(array &$form, FormStateInterface $form_state) { if ($form_state->getValue('confirm') && !empty($this->mediaItems)) { @@ -165,7 +173,7 @@ if ($delete_entities) { $this->storage->delete($delete_entities); - $this->logger('media')->notice('Deleted @count media entities.', ['@count' => count($delete_entities)]); + $this->logger('media')->notice('Deleted @count media items.', ['@count' => count($delete_entities)]); } if ($delete_translations) { @@ -185,12 +193,14 @@ } if ($total_count) { - drupal_set_message($this->formatPlural($total_count, 'Deleted 1 media entity.', 'Deleted @count media entities.')); + drupal_set_message($this->formatPlural($total_count, 'Deleted 1 media item.', 'Deleted @count media items.')); } $this->tempStoreFactory->get('media_multiple_delete_confirm')->delete(\Drupal::currentUser()->id()); } + // @todo Change to media library when #2834729 is done. + // https://www.drupal.org/node/2834729. $form_state->setRedirect('system.admin_content'); } 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 @@ -11,7 +11,7 @@ class MediaForm extends ContentEntityForm { /** - * Default settings for this media bundle. + * Default settings for this media type. * * @var array */ @@ -29,12 +29,12 @@ */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - /** @var \Drupal\media\MediaTypeInterface $bundle_entity */ - $bundle_entity = $this->entity->bundle->entity; + /** @var \Drupal\media\MediaTypeInterface $media_type */ + $media_type = $this->entity->bundle->entity; if ($this->operation === 'edit') { - $form['#title'] = $this->t('Edit %bundle_label @label', [ - '%bundle_label' => $bundle_entity->label(), + $form['#title'] = $this->t('Edit %type_label @label', [ + '%type_label' => $media_type->label(), '@label' => $this->entity->label(), ]); } 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 @@ -264,6 +264,24 @@ } /** + * Returns the source field storage definition. + * + * @return \Drupal\Core\Field\FieldStorageDefinitionInterface|null + * The field storage definition or NULL if it doesn't exists. + */ + protected function getSourceFieldStorage() { + // If we don't know the name of the source field, we can't get its storage. + if (empty($this->configuration['source_field'])) { + return NULL; + } + // Even if we do know the name of the source field, we cannot guarantee that + // its storage exists. So check for the storage. + $field = $this->configuration['source_field']; + $fields = $this->entityFieldManager->getFieldStorageDefinitions('media'); + return isset($fields[$field]) ? $fields[$field] : NULL; + } + + /** * Creates the source field definition for a type. * * @param \Drupal\media\MediaTypeInterface $type @@ -274,21 +292,12 @@ * should also be unsaved. */ protected function createSourceField(MediaTypeInterface $type) { - // Get field storage. - if (empty($this->configuration['source_field'])) { - // If we don't know the name of the source field, we need to - // create its storage. + // Get the storage or create if it doesn't exists yet. + $storage = $this->getSourceFieldStorage(); + if (is_null($storage)) { $storage = $this->createSourceFieldStorage(); $this->configuration['source_field'] = $storage->getName(); } - else { - // Even if we do know the name of the source field, we cannot guarantee - // its storage exists. So check for the storage and create it if needed. - $field = $this->configuration['source_field']; - $fields = $this->entityFieldManager->getFieldStorageDefinitions('media'); - $storage = isset($fields[$field]) ? $fields[$field] : $this->createSourceFieldStorage(); - } - return $this->entityTypeManager ->getStorage('field_config') ->create([ 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 @@ -17,10 +17,10 @@ interface MediaHandlerInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { /** - * Returns the display label of the media handler plugin. + * Returns the display label of the media handler. * * @return string - * The display label of the media handler plugin. + * The display label of the media handler. */ public function getLabel(); @@ -40,7 +40,7 @@ * Gets a media-related field/value. * * @param \Drupal\media\MediaInterface $media - * A media entity. + * A media item. * @param string $name * Name of field to fetch. * @@ -50,15 +50,15 @@ public function getField(MediaInterface $media, $name); /** - * Attaches handler-specific validation constraints to media. + * Attaches media handler-specific validation constraints to a media item. * * @param \Drupal\media\MediaInterface $media - * A media entity. + * A media item. */ public function attachConstraints(MediaInterface $media); /** - * Determines if the thumbnail should be updated for the media entity. + * Determines if the thumbnail should be updated for a media item. * * @param \Drupal\media\MediaInterface $media * A media item. @@ -73,12 +73,12 @@ /** * Gets thumbnail image. * - * Media handler plugin is responsible for returning file URI of the generic + * Media handler is responsible for returning file URI of the generic * thumbnail if no other is available. This function should always return a * valid file URI. * * @param \Drupal\media\MediaInterface $media - * A media entity. + * A media item. * * @return string * File URI of the thumbnail. @@ -96,12 +96,12 @@ /** * Provide a default name for the media item. * - * Plugins defining media bundles are suggested to override this method and + * Plugins defining a media handler are suggested to override this method and * provide a default name, to be used when there is no user-defined label * available. * * @param \Drupal\media\MediaInterface $media - * A media entity. + * A media item. * * @return string * A string that should be used as default media name. @@ -112,7 +112,7 @@ * Maps metadata field to the entity field. * * @param \Drupal\media\MediaInterface $media - * A media entity. + * A media item. * @param string $source_field * Name of the source metadata field. * @param string $destination_field diff -u b/core/modules/media/src/MediaInterface.php b/core/modules/media/src/MediaInterface.php --- b/core/modules/media/src/MediaInterface.php +++ b/core/modules/media/src/MediaInterface.php @@ -9,34 +9,34 @@ use Drupal\user\EntityOwnerInterface; /** - * Provides an interface defining a media entity. + * Provides an interface defining a entity for media items. */ interface MediaInterface extends ContentEntityInterface, EntityChangedInterface, RevisionLogInterface, EntityOwnerInterface, EntityPublishedInterface { /** - * Returns the media creation timestamp. + * Returns the media item creation timestamp. * * @return int - * Creation timestamp of the media. + * Creation timestamp of the media item. */ public function getCreatedTime(); /** - * Sets the media creation timestamp. + * Sets the media item creation timestamp. * * @param int $timestamp * The media creation timestamp. * * @return \Drupal\media\MediaInterface - * The called media entity. + * The called media item. */ public function setCreatedTime($timestamp); /** - * Returns the media handler plugin. + * Returns the media handler. * * @return $this - * The handler plugin. + * The media handler. */ public function getHandler(); 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 @@ -7,7 +7,7 @@ use Drupal\Core\StringTranslation\TranslationInterface; /** - * Provides a service for handling media thumbnails. + * Provides a service for handling media item thumbnails. */ class MediaThumbnailHandler implements MediaThumbnailHandlerInterface { @@ -63,7 +63,7 @@ } /** - * Gets a file URI for media. + * Gets a file URI for a media item. * * If thumbnail fetching should be queued then temporary use default * thumbnail for new files or temporary keep existing thumbnail for diff -u b/core/modules/media/src/MediaThumbnailHandlerInterface.php b/core/modules/media/src/MediaThumbnailHandlerInterface.php --- b/core/modules/media/src/MediaThumbnailHandlerInterface.php +++ b/core/modules/media/src/MediaThumbnailHandlerInterface.php @@ -8,7 +8,7 @@ interface MediaThumbnailHandlerInterface { /** - * Sets a media thumbnail. + * Update the thumbnail for a media item. * * @param \Drupal\media\MediaInterface $media * A media item. 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 @@ -134,7 +134,7 @@ ]; if (!$handler) { - $form['type']['#empty_option'] = $this->t('- Select handler -'); + $form['type']['#empty_option'] = $this->t('- Select media handler -'); } if ($handler) { @@ -247,7 +247,7 @@ } /** - * Gets subform state for the handler configuration subform. + * Gets subform state for the media handler configuration subform. * * @param array $form * Full form array. @@ -255,7 +255,7 @@ * Parent form state. * * @return \Drupal\Core\Form\SubFormStateInterface - * Sub-form state for the handler configuration form. + * Sub-form state for the media handler configuration form. */ protected function getHandlerSubFormState(array $form, FormStateInterface $form_state) { return SubformState::createForSubform($form['handler_dependent']['handler_configuration'], $form, $form_state) @@ -315,9 +315,9 @@ public function save(array $form, FormStateInterface $form_state) { $this->entity->set('auto_create_source_field', TRUE); $status = parent::save($form, $form_state); - $bundle = $this->entity; + $media_type = $this->entity; - $t_args = ['%name' => $bundle->label()]; + $t_args = ['%name' => $media_type->label()]; if ($status === SAVED_UPDATED) { drupal_set_message($this->t('The media type %name has been updated.', $t_args)); } @@ -326,16 +326,16 @@ $this->logger('media')->notice('Added type %name.', $t_args); } - // Override the "status" base field default value, for this bundle. - $fields = $this->entityFieldManager->getFieldDefinitions('media', $bundle->id()); + // Override the "status" base field default value, for this media type. + $fields = $this->entityFieldManager->getFieldDefinitions('media', $media_type->id()); /** @var \Drupal\media\MediaInterface $media */ - $media = $this->entityTypeManager->getStorage('media')->create(['bundle' => $bundle->id()]); + $media = $this->entityTypeManager->getStorage('media')->create(['bundle' => $media_type->id()]); $value = (bool) $form_state->getValue(['options', 'status']); if ($media->status->value != $value) { - $fields['status']->getConfig($bundle->id())->setDefaultValue($value)->save(); + $fields['status']->getConfig($media_type->id())->setDefaultValue($value)->save(); } - $form_state->setRedirectUrl($bundle->toUrl('collection')); + $form_state->setRedirectUrl($media_type->toUrl('collection')); } } 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 @@ -7,22 +7,11 @@ use Drupal\Core\Entity\RevisionableEntityBundleInterface; /** - * Provides an interface defining a media bundle entity. + * Provides an interface defining a media type entity. */ interface MediaTypeInterface extends ConfigEntityInterface, EntityDescriptionInterface, RevisionableEntityBundleInterface { /** - * Returns the label of the media type for a media item. - * - * @param \Drupal\media\MediaInterface $media - * A media item. - * - * @return string|bool - * Returns the label of the type of the media item. - */ - public static function getLabel(MediaInterface $media); - - /** * Returns whether thumbnail downloads are queued. * * @return bool @@ -42,10 +31,10 @@ public function setQueueThumbnailDownloadsStatus($queue_thumbnail_downloads); /** - * Returns the media handler plugin. + * Returns the media handler. * * @return \Drupal\media\MediaHandlerInterface - * The handler plugin. + * The media handler. */ public function getHandler(); 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 @@ -9,7 +9,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Redirects to a media deletion form. + * Redirects to a media item deletion form. * * @Action( * id = "media_delete_action", diff -u b/core/modules/media/src/Plugin/Action/PublishMedia.php b/core/modules/media/src/Plugin/Action/PublishMedia.php --- b/core/modules/media/src/Plugin/Action/PublishMedia.php +++ b/core/modules/media/src/Plugin/Action/PublishMedia.php @@ -7,7 +7,7 @@ use Drupal\media\Entity\Media; /** - * Publishes a media entity. + * Publishes a media item. * * @Action( * id = "media_publish_action", diff -u b/core/modules/media/src/Plugin/Action/UnpublishMedia.php b/core/modules/media/src/Plugin/Action/UnpublishMedia.php --- b/core/modules/media/src/Plugin/Action/UnpublishMedia.php +++ b/core/modules/media/src/Plugin/Action/UnpublishMedia.php @@ -7,7 +7,7 @@ use Drupal\media\Entity\Media; /** - * Unpublishes a media entity. + * Unpublishes a media item. * * @Action( * id = "media_unpublish_action", 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 @@ -113,7 +113,7 @@ $link_types = [ 'content' => $this->t('Linked to content'), - 'media' => $this->t('Linked to media entity'), + 'media' => $this->t('Linked to media item'), ]; // Display this setting only if image is linked. $image_link_setting = $this->getSetting('image_link'); @@ -182,7 +182,7 @@ */ public static function isApplicable(FieldDefinitionInterface $field_definition) { // This formatter is only available for entity types that reference - // media entities. + // media items. $target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type'); return $target_type == 'media'; } diff -u b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php --- b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php +++ b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php @@ -27,7 +27,7 @@ protected $thumbnailHandler; /** - * Constructs a new QueueWorkerBase object. + * Constructs a new ThumbnailDownloader object. * * @param array $configuration * A configuration array containing information about the plugin instance. diff -u b/core/modules/media/src/SourceFieldInterface.php b/core/modules/media/src/SourceFieldInterface.php --- b/core/modules/media/src/SourceFieldInterface.php +++ b/core/modules/media/src/SourceFieldInterface.php @@ -8,10 +8,10 @@ interface SourceFieldInterface extends MediaHandlerInterface { /** - * Returns the source field for a bundle using this plugin. + * Returns the source field for a media type using this media handler. * * @param \Drupal\media\MediaTypeInterface $type - * The media bundle. + * A media type. * * @return \Drupal\field\FieldConfigInterface * The source field definition. @@ -24,13 +24,13 @@ * Value that is stored in the source field. * * @param MediaInterface $media - * The media entity class. + * A media item. * * @return \Drupal\Core\TypedData\TypedDataInterface * The source value. * * @throws \RuntimeException - * If the source field for the handler is not defined. + * If the source field for the media handler is not defined. */ public function getSourceValue(MediaInterface $media); diff -u b/core/modules/media/templates/media.html.twig b/core/modules/media/templates/media.html.twig --- b/core/modules/media/templates/media.html.twig +++ b/core/modules/media/templates/media.html.twig @@ -1,19 +1,21 @@ {# /** * @file - * Default theme implementation to present a media entity. + * Default theme implementation to present a media item. * * Available variables: - * - media: The entity with limited access to object properties and methods. - * Only method names starting with "get", "has", or "is" and a few common - * methods such as "id", "label", and "bundle" are available. For example: + * - media: The media item with limited access to object properties and + * methods. Only method names starting with "get", "has", or "is" and + * a few common methods such as "id", "label", and "bundle" are available. + * For example: * - entity.getEntityTypeId() will return the entity type ID. * - entity.hasField('field_example') returns TRUE if the entity includes * field_example. (This does not indicate the presence of a value in this * field.) - * Calling other methods, such as entity.delete(), will result in an exception. + * Calling other methods, such as entity.delete(), will result in + * an exception. * See \Drupal\Core\Entity\EntityInterface for a full list of methods. - * - name: Name of the media. + * - name: Name of the media item. * - content: Media content. * - title_prefix: Additional output populated by modules, intended to be * displayed in front of the main title tag that appears in the template. diff -u b/core/modules/media/tests/modules/media_test_handler/config/schema/media_test_handler.schema.yml b/core/modules/media/tests/modules/media_test_handler/config/schema/media_test_handler.schema.yml --- b/core/modules/media/tests/modules/media_test_handler/config/schema/media_test_handler.schema.yml +++ b/core/modules/media/tests/modules/media_test_handler/config/schema/media_test_handler.schema.yml @@ -1,6 +1,6 @@ media.handler.test: type: media.handler.field_aware - label: 'Test handler configuration' + label: 'Test media handler configuration' mapping: test_config_value: type: string diff -u b/core/modules/media/tests/modules/media_test_type/media_test_type.info.yml b/core/modules/media/tests/modules/media_test_type/media_test_type.info.yml --- b/core/modules/media/tests/modules/media_test_type/media_test_type.info.yml +++ b/core/modules/media/tests/modules/media_test_type/media_test_type.info.yml @@ -1,6 +1,6 @@ name: 'Media test type' type: module -description: 'Provides test type for media entity.' +description: 'Provides test type for a media item.' core: 8.x package: Testing version: VERSION 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 @@ -13,18 +13,18 @@ class MediaAccessTest extends MediaFunctionalTestBase { /** - * The test media bundle. + * The test media type. * * @var \Drupal\media\MediaTypeInterface */ - protected $testBundle; + protected $testMediaType; /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->testBundle = $this->createMediaType(); + $this->testMediaType = $this->createMediaType(); } /** @@ -35,12 +35,12 @@ // Create media. $media = Media::create([ - 'bundle' => $this->testBundle->id(), + 'bundle' => $this->testMediaType->id(), 'name' => 'Unnamed', ]); $media->save(); $user_media = Media::create([ - 'bundle' => $this->testBundle->id(), + 'bundle' => $this->testMediaType->id(), 'name' => 'Unnamed', 'uid' => $this->nonAdminUser->id(), ]); @@ -66,10 +66,10 @@ $assert_session->statusCodeEquals(200); // Test 'create media' permission. - $this->drupalGet('media/add/' . $this->testBundle->id()); + $this->drupalGet('media/add/' . $this->testMediaType->id()); $assert_session->statusCodeEquals(403); $this->grantPermissions($role, ['create media']); - $this->drupalGet('media/add/' . $this->testBundle->id()); + $this->drupalGet('media/add/' . $this->testMediaType->id()); $assert_session->statusCodeEquals(200); // Test 'update media' and 'delete media' permissions. 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 @@ -20,18 +20,18 @@ public static $modules = ['media_test_views']; /** - * The test media bundle. + * The test media type. * * @var \Drupal\media\MediaTypeInterface */ - protected $testBundle; + protected $testMediaType; /** - * The test media entities. + * The test media items. * * @var \Drupal\media\MediaInterface[] */ - protected $mediaEntities; + protected $mediaItems; /** * {@inheritdoc} @@ -39,16 +39,16 @@ protected function setUp() { parent::setUp(); - $this->testBundle = $this->createMediaType(); + $this->testMediaType = $this->createMediaType(); - // Create some test media entities. - $this->mediaEntities = []; + // Create some test media items. + $this->mediaItems = []; for ($i = 1; $i <= 5; $i++) { $media = Media::create([ - 'bundle' => $this->testBundle->id(), + 'bundle' => $this->testMediaType->id(), ]); $media->save(); - $this->mediaEntities[] = $media; + $this->mediaItems[] = $media; } } @@ -60,7 +60,7 @@ $page = $session->getPage(); $assert_session = $this->assertSession(); - // Check that all created entities are present in the test view. + // Check that all created items are present in the test view. $view = Views::getView('test_media_bulk_form'); $view->execute(); $this->assertEquals($view->total_rows, 5); @@ -85,9 +85,9 @@ $page->selectFieldOption('action', 'media_unpublish_action'); $page->pressButton('Apply to selected items'); $assert_session->pageTextContains('Unpublish media was applied to 3 items'); - $this->assertFalse($this->storage->loadUnchanged(1)->isPublished(), 'The unpublish action failed in some of the media entities.'); - $this->assertFalse($this->storage->loadUnchanged(2)->isPublished(), 'The unpublish action failed in some of the media entities.'); - $this->assertFalse($this->storage->loadUnchanged(3)->isPublished(), 'The unpublish action failed in some of the media entities.'); + $this->assertFalse($this->storage->loadUnchanged(1)->isPublished(), 'The unpublish action failed in some of the media items.'); + $this->assertFalse($this->storage->loadUnchanged(2)->isPublished(), 'The unpublish action failed in some of the media items.'); + $this->assertFalse($this->storage->loadUnchanged(3)->isPublished(), 'The unpublish action failed in some of the media items.'); // Test publishing in bulk. $page->checkField('media_bulk_form[0]'); @@ -95,8 +95,8 @@ $page->selectFieldOption('action', 'media_publish_action'); $page->pressButton('Apply to selected items'); $assert_session->pageTextContains('Publish media was applied to 2 items'); - $this->assertTrue($this->storage->loadUnchanged(1)->isPublished(), 'The publish action failed in some of the media entities.'); - $this->assertTrue($this->storage->loadUnchanged(2)->isPublished(), 'The publish action failed in some of the media entities.'); + $this->assertTrue($this->storage->loadUnchanged(1)->isPublished(), 'The publish action failed in some of the media items.'); + $this->assertTrue($this->storage->loadUnchanged(2)->isPublished(), 'The publish action failed in some of the media items.'); // Test deletion in bulk. $page->checkField('media_bulk_form[0]'); @@ -105,9 +105,9 @@ $page->pressButton('Apply to selected items'); $assert_session->pageTextContains('Are you sure you want to delete these items?'); $page->pressButton('Delete'); - $assert_session->pageTextContains('Deleted 2 media entities.'); - $this->assertNull($this->storage->loadUnchanged(1), 'Could not delete some of the media entities.'); - $this->assertNull($this->storage->loadUnchanged(2), 'Could not delete some of the media entities.'); + $assert_session->pageTextContains('Deleted 2 media items.'); + $this->assertNull($this->storage->loadUnchanged(1), 'Could not delete some of the media items.'); + $this->assertNull($this->storage->loadUnchanged(2), 'Could not delete some of the media items.'); } } 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 @@ -15,8 +15,8 @@ * @param array $values * The media type values. * @param string $handler - * (optional) The handler plugin that is responsible for additional logic - * related to this media type. + * (optional) The media handler plugin that is responsible for additional + * logic related to this media type. * * @return \Drupal\media\MediaTypeInterface * A newly created media type. @@ -38,12 +38,12 @@ 'auto_create_source_field' => FALSE, ]; - $bundle = MediaType::create($values); - $status = $bundle->save(); + $media_type = MediaType::create($values); + $status = $media_type->save(); $this->assertEquals(SAVED_NEW, $status, 'Media type was created successfully.'); - return $bundle; + return $media_type; } } 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 @@ -22,11 +22,11 @@ ]; /** - * The test media bundle. + * The test media type. * * @var \Drupal\media\MediaTypeInterface */ - protected $testBundle; + protected $testMediaType; /** * {@inheritdoc} @@ -40,22 +40,22 @@ /** * Tests the media actions (add/edit/delete). */ - public function testMediaWithOnlyOneBundle() { + public function testMediaWithOnlyOneMediaType() { $session = $this->getSession(); $page = $session->getPage(); $assert_session = $this->assertSession(); - $bundle = $this->createMediaType([ + $media_type = $this->createMediaType([ 'new_revision' => FALSE, 'queue_thumbnail_downloads' => FALSE, ]); $this->drupalGet('media/add'); $assert_session->statusCodeEquals(200); - $assert_session->addressEquals('media/add/' . $bundle->id()); + $assert_session->addressEquals('media/add/' . $media_type->id()); $assert_session->elementNotExists('css', '#edit-revision'); - // Tests media item add form. + // Tests media add form. $media_name = $this->randomMachineName(); $page->fillField('name[0][value]', $media_name); $revision_log_message = $this->randomString(); @@ -71,8 +71,8 @@ $assert_session->titleEquals($media->label() . ' | Drupal'); // Tests media edit form. - $bundle->setNewRevision(FALSE); - $bundle->save(); + $media_type->setNewRevision(FALSE); + $media_type->save(); $media_name2 = $this->randomMachineName(); $this->drupalGet('media/' . $media_id . '/edit'); $assert_session->checkboxNotChecked('edit-revision'); @@ -84,7 +84,7 @@ // Test that there is no empty vertical tabs element, if the container is // empty (see #2750697). // Make the "Publisher ID" and "Created" fields hidden. - $this->drupalGet('/admin/structure/media/manage/' . $bundle->id() . '/form-display'); + $this->drupalGet('/admin/structure/media/manage/' . $media_type->id() . '/form-display'); $page->selectFieldOption('fields[created][parent]', 'hidden'); $page->selectFieldOption('fields[uid][parent]', 'hidden'); $page->pressButton('Save'); @@ -99,8 +99,8 @@ $this->drupalLogin($this->adminUser); // Enable revisions by default. - $bundle->setNewRevision(TRUE); - $bundle->save(); + $media_type->setNewRevision(TRUE); + $media_type->save(); $this->drupalGet('media/' . $media_id . '/edit'); $assert_session->checkboxChecked('edit-revision'); $page->fillField('name[0][value]', $media_name); @@ -126,31 +126,31 @@ * Tests the "media/add" and "media/mid" pages. * * Tests if the "media/add" page gives you a selecting option if there are - * multiple media bundles available. + * multiple media types available. */ - public function testMediaWithMultipleBundles() { + public function testMediaWithMultipleMediaTypes() { $assert_session = $this->assertSession(); - // Tests and creates the first media bundle. - $first_media_bundle = $this->createMediaType(['description' => $this->randomMachineName(32)]); + // Tests and creates the first media type. + $first_media_type = $this->createMediaType(['description' => $this->randomMachineName(32)]); - // Test and create a second media bundle. - $second_media_bundle = $this->createMediaType(['description' => $this->randomMachineName(32)]); + // Test and create a second media type. + $second_media_type = $this->createMediaType(['description' => $this->randomMachineName(32)]); - // Test if media/add displays two media bundle options. + // Test if media/add displays two media type options. $this->drupalGet('media/add'); - // Checks for the first media bundle. - $assert_session->pageTextContains($first_media_bundle->label()); - $assert_session->pageTextContains($first_media_bundle->getDescription()); - // Checks for the second media bundle. - $assert_session->pageTextContains($second_media_bundle->label()); - $assert_session->pageTextContains($second_media_bundle->getDescription()); + // Checks for the first media type. + $assert_session->pageTextContains($first_media_type->label()); + $assert_session->pageTextContains($first_media_type->getDescription()); + // Checks for the second media type. + $assert_session->pageTextContains($second_media_type->label()); + $assert_session->pageTextContains($second_media_type->getDescription()); - // Continue testing media bundle filter. - $first_media_item = Media::create(['bundle' => $first_media_bundle->id()]); + // Continue testing media type filter. + $first_media_item = Media::create(['bundle' => $first_media_type->id()]); $first_media_item->save(); - $second_media_item = Media::create(['bundle' => $second_media_bundle->id()]); + $second_media_item = Media::create(['bundle' => $second_media_type->id()]); $second_media_item->save(); // Go to first media item. @@ -165,14 +165,14 @@ } /** - * Tests the queue worker for media thumbnails. + * Tests the queue worker for media item thumbnails. */ public function testMediaThumbnailQueueWorker() { - // Tests and creates the media bundle with queued thumbnails. - $media_bundle = $this->createMediaType(['queue_thumbnail_downloads' => TRUE]); + // Tests and creates the media type with queued thumbnails. + $media_type = $this->createMediaType(['queue_thumbnail_downloads' => TRUE]); // Create a media item. - $media_item = Media::create(['bundle' => $media_bundle->id()]); + $media_item = Media::create(['bundle' => $media_type->id()]); $media_item->save(); $default_thumb_uri = $media_item->getHandler()->getDefaultThumbnail(); reverted: --- b/core/modules/media/tests/src/FunctionalJavascript/BundleCreationTest.php +++ /dev/null @@ -1,98 +0,0 @@ -drupalGet('admin/structure/media/add'); - $page = $this->getSession()->getPage(); - - // Fill in a label to the bundle. - $page->fillField('label', $label); - // Wait for machine name generation. Default: waitUntilVisible(), does not - // work properly. - $this->getSession() - ->wait(5000, "jQuery('.machine-name-value').text() === '{$bundleMachineName}'"); - - // Select our test bundle type. - $this->assertSession()->fieldExists('Handler'); - $this->assertSession()->optionExists('Handler', 'test'); - $page->selectFieldOption('Handler', 'test'); - $this->assertSession()->assertWaitOnAjaxRequest(); - - $page->pressButton('Save'); - - // Check whether the source field was correctly created. - $this->drupalGet("admin/structure/media/manage/{$bundleMachineName}/fields"); - - // Check 2nd column of first data row, to be machine name for field name. - $this->assertSession() - ->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test'); - // Check 3rd column of first data row, to be correct field type. - $this->assertSession() - ->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)'); - - // Check that the source field is correctly assigned to media bundle. - $this->drupalGet("admin/structure/media/manage/{$bundleMachineName}"); - - $this->assertSession()->pageTextContains('Test handler field is used to store the essential information about the media item.'); - } - - /** - * Test creation of media bundle, reusing an existing source field. - */ - public function testBundleCreationReuseSourceField() { - // Create a new bundle, which should create a new field we can reuse. - $this->drupalGet('/admin/structure/media/add'); - $page = $this->getSession()->getPage(); - $page->fillField('label', 'Pastafazoul'); - $this->getSession() - ->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'"); - $page->selectFieldOption('Handler', 'test'); - $this->assertSession()->assertWaitOnAjaxRequest(); - $page->pressButton('Save'); - - $label = 'Bundle reusing Default Field'; - $bundleMachineName = str_replace(' ', '_', strtolower($label)); - - $this->drupalGet('admin/structure/media/add'); - $page = $this->getSession()->getPage(); - - // Fill in a label to the bundle. - $page->fillField('label', $label); - - // Wait for machine name generation. Default: waitUntilVisible(), does not - // work properly. - $this->getSession() - ->wait(5000, "jQuery('.machine-name-value').text() === '{$bundleMachineName}'"); - - // Select our test bundle type. - $this->assertSession()->fieldExists('Handler'); - $this->assertSession()->optionExists('Handler', 'test'); - $page->selectFieldOption('Handler', 'test'); - $this->assertSession()->assertWaitOnAjaxRequest(); - // Select the existing field for re-use. - $page->selectFieldOption('handler_configuration[source_field]', 'field_media_test'); - $page->pressButton('Save'); - - // Check that there are not fields created. - $this->drupalGet("admin/structure/media/manage/{$bundleMachineName}/fields"); - // The reused field should be present... - $this->assertSession()->pageTextContains('field_media_test'); - // ...not a new, unique one. - $this->assertSession()->pageTextNotContains('field_media_test_1'); - } - -} 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 @@ -26,7 +26,7 @@ * * @var \Drupal\media\MediaTypeInterface */ - protected $testBundle; + protected $testMediaType; /** * {@inheritdoc} @@ -38,9 +38,9 @@ } /** - * Tests a media bundle administration. + * Tests a media type administration. */ - public function testMediaBundles() { + public function testMediaTypes() { $session = $this->getSession(); $page = $session->getPage(); $assert_session = $this->assertSession(); @@ -50,7 +50,7 @@ $assert_session->pageTextContains('No media types available. Add media type.'); $assert_session->linkExists('Add media type'); - // Test the creation of a media bundle using the UI. + // Test the creation of a media type using the UI. $name = $this->randomMachineName(); $description = $this->randomMachineName(); $this->drupalGet('admin/structure/media/add'); @@ -68,16 +68,16 @@ $assert_session->pageTextContains($name); $assert_session->pageTextContains($description); - /** @var \Drupal\media\MediaTypeInterface $bundle_storage */ - $bundle_storage = $this->container->get('entity_type.manager')->getStorage('media_type'); - $this->testBundle = $bundle_storage->load(strtolower($name)); + /** @var \Drupal\media\MediaTypeInterface $media_type_storage */ + $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type'); + $this->testMediaType = $media_type_storage->load(strtolower($name)); // Check if all action links exist. $assert_session->linkByHrefExists('admin/structure/media/add'); - $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testBundle->id()); - $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testBundle->id() . '/fields'); - $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testBundle->id() . '/form-display'); - $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testBundle->id() . '/display'); + $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id()); + $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/fields'); + $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/form-display'); + $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/display'); // Assert that fields have expected values before editing. $page->clickLink('Edit'); @@ -105,7 +105,7 @@ // Test if the edit machine name is not editable. $assert_session->fieldDisabled('Machine-readable name'); - // Edit and save media bundle form fields with new values. + // Edit and save media type form fields with new values. $new_name = $this->randomMachineName(); $new_description = $this->randomMachineName(); $page->fillField('label', $new_name); @@ -120,7 +120,7 @@ $assert_session->statusCodeEquals(200); // Test if edit worked and if new field values have been saved as expected. - $this->drupalGet('admin/structure/media/manage/' . $this->testBundle->id()); + $this->drupalGet('admin/structure/media/manage/' . $this->testMediaType->id()); $assert_session->fieldValueEquals('label', $new_name); $assert_session->fieldValueEquals('description', $new_description); $assert_session->fieldValueEquals('handler', 'test'); @@ -131,47 +131,47 @@ $assert_session->fieldValueEquals('Field 1', 'name'); $assert_session->fieldValueEquals('Field 2', '_none'); - /** @var \Drupal\media\MediaTypeInterface $loaded_bundle */ - $loaded_bundle = $this->container->get('entity_type.manager') + /** @var \Drupal\media\MediaTypeInterface $loaded_media_type */ + $loaded_media_type = $this->container->get('entity_type.manager') ->getStorage('media_type') - ->load($this->testBundle->id()); - $this->assertEquals($loaded_bundle->id(), $this->testBundle->id()); - $this->assertEquals($loaded_bundle->label(), $new_name); - $this->assertEquals($loaded_bundle->getDescription(), $new_description); - $this->assertEquals($loaded_bundle->getHandler()->getPluginId(), 'test'); - $this->assertEquals($loaded_bundle->getHandler()->getConfiguration()['test_config_value'], 'This is new config value.'); - $this->assertTrue($loaded_bundle->shouldCreateNewRevision()); - $this->assertTrue($loaded_bundle->thumbnailDownloadsAreQueued()); - $this->assertFalse($loaded_bundle->getStatus()); - $this->assertEquals($loaded_bundle->getFieldMap(), ['field_1' => 'name']); + ->load($this->testMediaType->id()); + $this->assertEquals($loaded_media_type->id(), $this->testMediaType->id()); + $this->assertEquals($loaded_media_type->label(), $new_name); + $this->assertEquals($loaded_media_type->getDescription(), $new_description); + $this->assertEquals($loaded_media_type->getHandler()->getPluginId(), 'test'); + $this->assertEquals($loaded_media_type->getHandler()->getConfiguration()['test_config_value'], 'This is new config value.'); + $this->assertTrue($loaded_media_type->shouldCreateNewRevision()); + $this->assertTrue($loaded_media_type->thumbnailDownloadsAreQueued()); + $this->assertFalse($loaded_media_type->getStatus()); + $this->assertEquals($loaded_media_type->getFieldMap(), ['field_1' => 'name']); // We need to clear the statically cached field definitions to account for // fields that have been created by API calls in this test, since they exist // in a separate memory space from the web server. $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); - // Test that a media being created with default status to "FALSE" will be - // created unpublished. + // Test that a media item being created with default status to "FALSE", + // will be created unpublished. /** @var \Drupal\media\MediaInterface $unpublished_media */ - $unpublished_media = Media::create(['name' => 'unpublished test media', 'bundle' => $loaded_bundle->id()]); + $unpublished_media = Media::create(['name' => 'unpublished test media', 'bundle' => $loaded_media_type->id()]); $this->assertFalse($unpublished_media->isPublished()); $unpublished_media->delete(); - // Tests media bundle delete form. + // Tests media type delete form. $page->clickLink('Delete'); - $assert_session->addressEquals('admin/structure/media/manage/' . $this->testBundle->id() . '/delete'); + $assert_session->addressEquals('admin/structure/media/manage/' . $this->testMediaType->id() . '/delete'); $page->pressButton('Delete'); $assert_session->addressEquals('admin/structure/media'); $assert_session->pageTextContains('The media type ' . $new_name . ' has been deleted.'); - // Test bundle delete prevention when there is existing media. - $bundle2 = $this->createMediaType(['auto_create_source_field' => TRUE]); - $label2 = $bundle2->label(); - $media = Media::create(['name' => 'lorem ipsum', 'bundle' => $bundle2->id()]); + // Test type delete prevention when there is existing media. + $media_type2 = $this->createMediaType(['auto_create_source_field' => TRUE]); + $label2 = $media_type2->label(); + $media = Media::create(['name' => 'lorem ipsum', 'bundle' => $media_type2->id()]); $media->save(); - $this->drupalGet('admin/structure/media/manage/' . $bundle2->id()); + $this->drupalGet('admin/structure/media/manage/' . $media_type2->id()); $page->clickLink('Delete'); - $assert_session->addressEquals('admin/structure/media/manage/' . $bundle2->id() . '/delete'); + $assert_session->addressEquals('admin/structure/media/manage/' . $media_type2->id() . '/delete'); $assert_session->fieldNotExists('edit-submit'); $assert_session->pageTextContains("$label2 is used by 1 media item on your site. You can not remove this media type until you have removed all of the $label2 media items."); } 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 @@ -10,7 +10,7 @@ use Drupal\media\MediaTypeInterface; /** - * Tests creation of Media Bundles and Media Entities. + * Tests creation of media types and media items. * * @group media */ @@ -36,7 +36,7 @@ * * @var \Drupal\media\MediaTypeInterface */ - protected $testBundle; + protected $testMediaType; /** * {@inheritdoc} @@ -50,45 +50,45 @@ $this->installEntitySchema('media'); $this->installConfig(['field', 'system', 'image', 'file']); - // Create a test bundle. + // Create a test media type. $id = strtolower($this->randomMachineName()); - $this->testBundle = MediaType::create([ + $this->testMediaType = MediaType::create([ 'id' => $id, 'label' => $id, 'handler' => 'test', 'new_revision' => FALSE, 'auto_create_source_field' => TRUE, ]); - $this->testBundle->save(); + $this->testMediaType->save(); } /** - * Tests creating a media bundle programmatically. + * Tests creating a media type programmatically. */ - public function testMediaBundleCreation() { - $bundle_storage = $this->container->get('entity_type.manager')->getStorage('media_type'); + public function testMediaTypeCreation() { + $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type'); - $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testBundle->id()), 'The new media type has not been correctly created in the database.'); + $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType->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->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.'); - // Source field is not set on the handler, but it should never be created - // automatically when a config is being imported. - $this->assertEquals(['source_field' => '', 'test_config_value' => 'Kakec'], $test_bundle->get('handler_configuration'), 'Could not assure the correct handler configuration.'); - $this->assertEquals([], $test_bundle->get('field_map'), 'Could not assure the correct field map.'); + $test_media_type = $media_type_storage->load('test'); + $this->assertInstanceOf(MediaTypeInterface::class, $test_media_type, 'The media type from default configuration has not been created in the database.'); + $this->assertEquals('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.'); + $this->assertEquals('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.'); + $this->assertEquals('test', $test_media_type->get('handler'), 'Could not assure the correct media handler.'); + // Source field is not set on the media handler, but it should never + // be created automatically when a config is being imported. + $this->assertEquals(['source_field' => '', 'test_config_value' => 'Kakec'], $test_media_type->get('handler_configuration'), 'Could not assure the correct media handler configuration.'); + $this->assertEquals([], $test_media_type->get('field_map'), 'Could not assure the correct field map.'); } /** - * Tests creating a media entity programmatically. + * Tests creating a media item programmatically. */ public function testMediaEntityCreation() { $media = Media::create([ - 'bundle' => $this->testBundle->id(), + 'bundle' => $this->testMediaType->id(), 'name' => 'Unnamed', 'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.', ]); @@ -96,83 +96,83 @@ $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.'); - $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.'); + $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.'); + $this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.'); + $this->assertEquals('Unnamed', $media->label(), 'The media item was not created with the correct name.'); $this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->bundle->entity->getHandler()->getSourceValue($media)->getValue(), 'Handler returns correct source value.'); - // Test the creation of a media without user-defined label and check if a - // default name is provided. + // Test the creation of a media item without user-defined label and + // check if a default name is provided. $media = Media::create([ - 'bundle' => $this->testBundle->id(), + 'bundle' => $this->testMediaType->id(), ]); $media->save(); - $expected_name = 'media' . ':' . $this->testBundle->id() . ':' . $media->uuid(); - $this->assertEquals($this->testBundle->id(), $media->bundle(), 'The media was not created with correct type.'); - $this->assertEquals($expected_name, $media->label(), 'The media was not created with a default name.'); + $expected_name = 'media' . ':' . $this->testMediaType->id() . ':' . $media->uuid(); + $this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with correct type.'); + $this->assertEquals($expected_name, $media->label(), 'The media item was not created with a default name.'); } /** - * Tests creating and updating bundles programmatically. + * Tests creating and updating media type programmatically. */ - public function testProgrammaticBundleManipulation() { - // Creating a bundle programmatically without specifying a source field + public function testProgrammaticMediaTypeManipulation() { + // Creating a media type programmatically without specifying a source field // should create one automagically. /** @var FieldConfig $field */ - $field = $this->testBundle->getHandler()->getSourceField($this->testBundle); + $field = $this->testMediaType->getHandler()->getSourceField($this->testMediaType); $this->assertInstanceOf(FieldConfig::class, $field); $this->assertEquals('field_media_test', $field->getName()); $this->assertFalse($field->isNew()); // Saving with a non-existent source field should create it if the related // flag is set. - $this->testBundle->getHandler()->setConfiguration([ + $this->testMediaType->getHandler()->setConfiguration([ 'source_field' => 'field_magick', ]); - $this->testBundle->set('auto_create_source_field', TRUE); - $this->testBundle->save(); - $field = $this->testBundle->getHandler()->getSourceField($this->testBundle); + $this->testMediaType->set('auto_create_source_field', TRUE); + $this->testMediaType->save(); + $field = $this->testMediaType->getHandler()->getSourceField($this->testMediaType); $this->assertInstanceOf(FieldConfig::class, $field); $this->assertEquals('field_magick', $field->getName()); $this->assertFalse($field->isNew()); // Trying to save without a source field should create a new, de-duped one. - $this->testBundle->getHandler()->setConfiguration([]); - $this->testBundle->set('auto_create_source_field', TRUE); - $this->testBundle->save(); - $field = $this->testBundle->getHandler()->getSourceField($this->testBundle); + $this->testMediaType->getHandler()->setConfiguration([]); + $this->testMediaType->set('auto_create_source_field', TRUE); + $this->testMediaType->save(); + $field = $this->testMediaType->getHandler()->getSourceField($this->testMediaType); $this->assertInstanceOf(FieldConfig::class, $field); $this->assertEquals('field_media_test_1', $field->getName()); $this->assertFalse($field->isNew()); // Trying to reuse an existing field should, well, reuse the existing field // even if the auto create flag is set. - $this->testBundle->getHandler()->setConfiguration([ + $this->testMediaType->getHandler()->setConfiguration([ 'source_field' => 'field_magick', ]); - $this->testBundle->set('auto_create_source_field', TRUE); - $this->testBundle->save(); - $field = $this->testBundle->getHandler()->getSourceField($this->testBundle); + $this->testMediaType->set('auto_create_source_field', TRUE); + $this->testMediaType->save(); + $field = $this->testMediaType->getHandler()->getSourceField($this->testMediaType); $this->assertInstanceOf(FieldConfig::class, $field); $this->assertEquals('field_magick', $field->getName()); $this->assertFalse($field->isNew()); // No new de-duped fields should have been created. $duplicates = FieldConfig::loadMultiple([ - 'media.' . $this->testBundle->id() . '.field_magick_1', - 'media.' . $this->testBundle->id() . '.field_media_generic_2', + 'media.' . $this->testMediaType->id() . '.field_magick_1', + 'media.' . $this->testMediaType->id() . '.field_media_generic_2', ]); $this->assertEmpty($duplicates); // If auto-create flag is not set fields shouldn't be created. // ... if an imaginary field is set. - $this->testBundle->getHandler()->setConfiguration([ + $this->testMediaType->getHandler()->setConfiguration([ 'source_field' => 'field_imaginary', ]); - $this->testBundle->set('auto_create_source_field', FALSE); - $this->testBundle->save(); + $this->testMediaType->set('auto_create_source_field', FALSE); + $this->testMediaType->save(); $field = $this->container->get('entity_type.manager') ->getStorage('field_config') - ->load('media.' . $this->testBundle->id() . '.field_imaginary'); + ->load('media.' . $this->testMediaType->id() . '.field_imaginary'); $field_storage = $this->container->get('entity_type.manager') ->getStorage('field_storage_config') ->load('media.field_imaginary'); @@ -180,19 +180,19 @@ $this->assertNull($field_storage, 'Field was not automatically created.'); // ... if no field is set. - $this->testBundle->getHandler()->setConfiguration([]); - $this->testBundle->set('auto_create_source_field', FALSE); - $this->testBundle->save(); - $config = $this->testBundle->getHandler()->getConfiguration(); + $this->testMediaType->getHandler()->setConfiguration([]); + $this->testMediaType->set('auto_create_source_field', FALSE); + $this->testMediaType->save(); + $config = $this->testMediaType->getHandler()->getConfiguration(); $this->assertEquals('', $config['source_field'], 'Source field value was not automatically populated.'); // Check what would the field be named like if it was created. - $proposed_field = $this->testBundle->getHandler()->getSourceField($this->testBundle); + $proposed_field = $this->testMediaType->getHandler()->getSourceField($this->testMediaType); $this->assertInstanceOf(FieldConfig::class, $proposed_field); $this->assertEquals('field_media_test_2', $proposed_field->getName()); $this->assertTrue($proposed_field->isNew()); $field = $this->container->get('entity_type.manager') ->getStorage('field_config') - ->load('media.' . $this->testBundle->id() . '.' . $proposed_field->getName()); + ->load('media.' . $this->testMediaType->id() . '.' . $proposed_field->getName()); $field_storage = $this->container->get('entity_type.manager') ->getStorage('field_storage_config') ->load('media.' . $proposed_field->getName()); 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 @@ -42,12 +42,12 @@ * Tests some of the tokens provided by Media. */ public function testMediaEntityTokens() { - // Create a test media bundle. - $bundle_name = $this->randomMachineName(); + // Create a test media type. + $media_type_name = $this->randomMachineName(); MediaType::create([ - 'id' => $bundle_name, - 'label' => $bundle_name, + 'id' => $media_type_name, + 'label' => $media_type_name, 'handler' => 'test', 'handler_configuration' => ['test_config_value' => 'Kakec'], 'field_map' => [], @@ -55,10 +55,10 @@ 'new_revision' => FALSE, ])->save(); - // Create a media entity. + // Create a media item. $media = Media::create([ 'name' => $this->randomMachineName(), - 'bundle' => $bundle_name, + 'bundle' => $media_type_name, 'uid' => '1', 'langcode' => Language::LANGCODE_DEFAULT, 'status' => TRUE, only in patch2: unchanged: --- /dev/null +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php @@ -0,0 +1,98 @@ +drupalGet('admin/structure/media/add'); + $page = $this->getSession()->getPage(); + + // Fill in a label to the media type. + $page->fillField('label', $label); + // Wait for machine name generation. Default: waitUntilVisible(), does not + // work properly. + $this->getSession() + ->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'"); + + // Select the media handler used by our media type. + $this->assertSession()->fieldExists('Handler'); + $this->assertSession()->optionExists('Handler', 'test'); + $page->selectFieldOption('Handler', 'test'); + $this->assertSession()->assertWaitOnAjaxRequest(); + + $page->pressButton('Save'); + + // Check whether the source field was correctly created. + $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields"); + + // Check 2nd column of first data row, to be machine name for field name. + $this->assertSession() + ->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test'); + // Check 3rd column of first data row, to be correct field type. + $this->assertSession() + ->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)'); + + // Check that the source field is correctly assigned to media type. + $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}"); + + $this->assertSession()->pageTextContains('Test handler field is used to store the essential information about the media item.'); + } + + /** + * Test creation of media type, reusing an existing source field. + */ + public function testMediaTypeCreationReuseSourceField() { + // Create a new media type, which should create a new field we can reuse. + $this->drupalGet('/admin/structure/media/add'); + $page = $this->getSession()->getPage(); + $page->fillField('label', 'Pastafazoul'); + $this->getSession() + ->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'"); + $page->selectFieldOption('Handler', 'test'); + $this->assertSession()->assertWaitOnAjaxRequest(); + $page->pressButton('Save'); + + $label = 'Type reusing Default Field'; + $mediaTypeMachineName = str_replace(' ', '_', strtolower($label)); + + $this->drupalGet('admin/structure/media/add'); + $page = $this->getSession()->getPage(); + + // Fill in a label to the media type. + $page->fillField('label', $label); + + // Wait for machine name generation. Default: waitUntilVisible(), does not + // work properly. + $this->getSession() + ->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'"); + + // Select the media handler used by our media type. + $this->assertSession()->fieldExists('Handler'); + $this->assertSession()->optionExists('Handler', 'test'); + $page->selectFieldOption('Handler', 'test'); + $this->assertSession()->assertWaitOnAjaxRequest(); + // Select the existing field for re-use. + $page->selectFieldOption('handler_configuration[source_field]', 'field_media_test'); + $page->pressButton('Save'); + + // Check that there are not fields created. + $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields"); + // The reused field should be present... + $this->assertSession()->pageTextContains('field_media_test'); + // ...not a new, unique one. + $this->assertSession()->pageTextNotContains('field_media_test_1'); + } + +}