diff --git a/core/modules/media/src/Annotation/MediaSource.php b/core/modules/media/src/Annotation/MediaSource.php index 2440ebec53..a1ab2231c4 100644 --- a/core/modules/media/src/Annotation/MediaSource.php +++ b/core/modules/media/src/Annotation/MediaSource.php @@ -80,14 +80,19 @@ class MediaSource extends Plugin { public $thumbnail_uri_metadata_attribute = 'thumbnail_uri'; /** - * The metadata attribute name to provide the thumbnail alt. + * (optional) The metadata attribute name to provide the thumbnail alt. + * + * "Thumbnail" will be used if the attribute name is not provided. * * @var string|null */ public $thumbnail_alt_metadata_attribute; /** - * The metadata attribute name to provide the thumbnail title. + * (optional) The metadata attribute name to provide the thumbnail title. + * + * The name of the media entity will be used if the attribute name is not + * provided. * * @var string|null */ diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index e6538a4706..f9c06093ac 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -139,7 +139,7 @@ public function getSource() { * Update the thumbnail for the media item. * * @param bool $from_queue - * Specifies whether the thumbnail is being fetched from the queue. + * Specifies whether the thumbnail update is triggered from the queue. * * @return \Drupal\media\MediaInterface * The updated media item. @@ -165,7 +165,7 @@ protected function updateThumbnail($from_queue = FALSE) { $this->thumbnail->target_id = $file->id(); } - // Set the image alt. + // Set the thumbnail alt. $plugin_definition = $this->getSource()->getPluginDefinition(); if (!empty($plugin_definition['thumbnail_alt_metadata_attribute'])) { $this->thumbnail->alt = $this->getSource()->getMetadata($this, $plugin_definition['thumbnail_alt_metadata_attribute']); @@ -174,7 +174,7 @@ protected function updateThumbnail($from_queue = FALSE) { $this->thumbnail->alt = $this->t('Thumbnail'); } - // Set the image title. + // Set the thumbnail title. if (!empty($plugin_definition['thumbnail_title_metadata_attribute'])) { $this->thumbnail->title = $this->getSource()->getMetadata($this, $plugin_definition['thumbnail_title_metadata_attribute']); } diff --git a/core/modules/media/src/MediaSourceInterface.php b/core/modules/media/src/MediaSourceInterface.php index 95774c8cb9..1036907507 100644 --- a/core/modules/media/src/MediaSourceInterface.php +++ b/core/modules/media/src/MediaSourceInterface.php @@ -62,6 +62,8 @@ * @see \Drupal\media\MediaSourceBase * @see \Drupal\media\MediaSourceManager * @see \Drupal\media\MediaTypeInterface + * @see \Drupal\media\MediaSourceEntityConstraintsInterface + * @see \Drupal\media\MediaSourceFieldConstraintsInterface * @see plugin_api */ interface MediaSourceInterface extends PluginInspectionInterface, ConfigurablePluginInterface, PluginFormInterface { diff --git a/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php index b79605c47a..d30a232da1 100644 --- a/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php +++ b/core/modules/media/src/Plugin/QueueWorker/ThumbnailDownloader.php @@ -5,7 +5,6 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\media\Entity\Media; use Drupal\Core\Queue\QueueWorkerBase; -use Drupal\media\MediaThumbnailHandlerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** diff --git a/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php index 2e6653559d..b93e482ded 100644 --- a/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php +++ b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/Test.php @@ -27,7 +27,9 @@ public function getMetadataAttributes() { 'attribute_1' => ['label' => $this->t('Attribute 1'), 'value' => 'Value 1'], 'attribute_2' => ['label' => $this->t('Attribute 2'), 'value' => 'Value 1'], ]); - return array_map(function ($item) {return $item['label'];}, $attributes); + return array_map(function ($item) { + return $item['label']; + }, $attributes); } /** diff --git a/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php index f6b4a69514..365370987f 100644 --- a/core/modules/media/tests/src/Kernel/MediaSourceTest.php +++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php @@ -3,6 +3,8 @@ namespace Drupal\Tests\media\Kernel; use Drupal\Core\Entity\EntityStorageException; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\media\Entity\Media; use Drupal\media\Entity\MediaType; @@ -55,22 +57,18 @@ public function testDefaultName() { public function testMetadataMapping() { $field_name = 'field_to_map_to'; $attribute_name = 'attribute_to_map'; - $storage = $this->container->get('entity_type.manager') - ->getStorage('field_storage_config') - ->create([ - 'entity_type' => 'media', - 'field_name' => $field_name, - 'type' => 'string', - ]); + $storage = FieldStorageConfig::create([ + 'entity_type' => 'media', + 'field_name' => $field_name, + 'type' => 'string', + ]); $storage->save(); - $this->container->get('entity_type.manager') - ->getStorage('field_config') - ->create([ - 'field_storage' => $storage, - 'bundle' => $this->testMediaType->id(), - 'label' => 'Field to map to', - ])->save(); + FieldConfig::create([ + 'field_storage' => $storage, + 'bundle' => $this->testMediaType->id(), + 'label' => 'Field to map to', + ])->save(); // Save the entity without defining the metadata mapping and check that the // field stays empty. @@ -84,7 +82,7 @@ public function testMetadataMapping() { // Define mapping and make sure that the value was stored in the field. \Drupal::state()->set('media_source_test_attributes', [ - $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'] + $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'], ]); $this->testMediaType->setFieldMap([$attribute_name => $field_name])->save(); $media = Media::create([ @@ -98,7 +96,7 @@ public function testMetadataMapping() { // Change the metadata attribute value and re-save the entity. Field value // should stay the same. \Drupal::state()->set('media_source_test_attributes', [ - $attribute_name => ['title' => 'Attribute to map', 'value' => 'Pinkeye'] + $attribute_name => ['title' => 'Attribute to map', 'value' => 'Pinkeye'], ]); $this->assertEquals('Pinkeye', $media->getSource()->getMetadata($media, $attribute_name), 'Value of the metadata attribute is correct.'); $media->save(); @@ -110,10 +108,20 @@ public function testMetadataMapping() { $media->set('field_media_test', 'some_new_value'); $media->save(); $this->assertEquals('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was correctly mapped to the field.'); + + // Remove the value of the mapped field and make sure that it is re-mapped + // on save. + \Drupal::state()->set('media_source_test_attributes', [ + $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'], + ]); + $media->{$field_name}->value = NULL; + $this->assertEquals('Snowball', $media->getSource()->getMetadata($media, $attribute_name), 'Value of the metadata attribute is correct.'); + $media->save(); + $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was correctly mapped to the field.'); } /** - * Tests the default thumbnail functionality. + * Tests the thumbnail functionality. */ public function testThumbnail() { file_put_contents('public://thumbnail1.jpg', ''); @@ -143,15 +151,19 @@ public function testThumbnail() { $this->assertEquals('public://thumbnail2.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is correct.'); $media->save(); $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was preserved.'); + $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was set on the thumbnail.'); + $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was set on the thumbnail.'); // Remove the thumbnail and make sure that it is auto-updated on save. $media->thumbnail->target_id = NULL; $this->assertEquals('public://thumbnail2.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is correct.'); $media->save(); $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was added to the media entity.'); + $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was set on the thumbnail.'); + $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was set on the thumbnail.'); - // Change the metadata attribute again, change the source field value and - // make sure that the thumbnail updates too. + // Change the metadata attribute again, change the source field value too + // and make sure that the thumbnail updates. \Drupal::state()->set('media_source_test_attributes', [ 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], ]); @@ -159,6 +171,8 @@ public function testThumbnail() { $this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is correct.'); $media->save(); $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was added to the media entity.'); + $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was set on the thumbnail.'); + $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was set on the thumbnail.'); // Change the thumbnail metadata attribute and make sure that the thumbnail // is set correctly. @@ -176,6 +190,8 @@ public function testThumbnail() { $this->assertEquals('public://thumbnail2.jpg', $media->getSource()->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is correct.'); $media->save(); $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was used for the thumbnail.'); + $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was set on the thumbnail.'); + $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was set on the thumbnail.'); // Enable queued thumbnails and make sure that the entity gets the default // thumbnail initially. @@ -192,6 +208,8 @@ public function testThumbnail() { $this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is correct.'); $media->save(); $this->assertEquals('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was set initially.'); + $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was set on the thumbnail.'); + $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was set on the thumbnail.'); // Process the queue item and make sure that the thumbnail was updated too. $queue_name = 'media_entity_thumbnail'; @@ -209,6 +227,8 @@ public function testThumbnail() { $media = Media::load($media->id()); $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was updated by the queue.'); + $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was set on the thumbnail.'); + $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was set on the thumbnail.'); // Set alt and title metadata attributes and make sure they are used for the // thumbnail. @@ -240,8 +260,8 @@ public function testConstraints() { 'MediaTestConstraint' => [], ]); - // Create media that uses source with constraints and make sure it can't - // be saved without validating them. + // Create media that uses source plugin with constraints and make sure it + // can't be saved without validating them. /** @var \Drupal\media\MediaInterface $media */ $media = Media::create([ 'bundle' => $this->testConstraintsMediaType->id(), @@ -342,17 +362,15 @@ public function testSourceFieldCreation() { $this->assertEquals('Test source', $field->label(), 'Correct label is used.'); $this->assertEquals('test_type', $field->getTargetBundle(), 'Field is targeting correct bundle.'); - // Fields should be automatically saved only when using a form. + // Fields should be automatically saved only when creating the media type + // using the media type creation form. Make sure that they are not saved + // when creating a media type programatically. // Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing // form part of the functionality. $type->save(); - $storage = $this->container->get('entity_type.manager') - ->getStorage('field_storage_config') - ->load('media.field_media_test_1'); + $storage = FieldStorageConfig::load('media.field_media_test_1'); $this->assertNull($storage, 'Field storage was not saved.'); - $field = $this->container->get('entity_type.manager') - ->getStorage('field_config') - ->load('media.test_type.field_media_test_1'); + $field = FieldConfig::load('media.test_type.field_media_test_1'); $this->assertNull($field, 'Field storage was not saved.'); // Test the plugin with a different default source field type.