summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-12-04 13:45:14 (GMT)
committerAlex Pott2018-12-04 13:45:23 (GMT)
commitc2c4d3d04abd3a83b3f46b1edc7535bfabd24033 (patch)
tree54e13328406e8aa489cc2476b11fe99a313a0710
parentad9f72e1bd2ea8c688ab00647e1a2e6be105c942 (diff)
Issue #2956368 by marcoscano, Nitebreed, ph53: MediaThumbnailFormatter produces unhelpful text alternative and title attributes for media thumbnails
(cherry picked from commit e6dfd046db36a028782fdbd5139f27588c027b05)
-rw-r--r--core/modules/media/src/Entity/Media.php13
-rw-r--r--core/modules/media/src/Plugin/media/Source/Image.php6
-rw-r--r--core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php4
-rw-r--r--core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php2
-rw-r--r--core/modules/media/tests/src/Kernel/MediaSourceTest.php53
-rw-r--r--core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php2
6 files changed, 37 insertions, 43 deletions
diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php
index c218210..d68a0a4 100644
--- a/core/modules/media/src/Entity/Media.php
+++ b/core/modules/media/src/Entity/Media.php
@@ -185,20 +185,11 @@ class Media extends EditorialContentEntityBase implements MediaInterface {
// Set the thumbnail alt.
$media_source = $this->getSource();
$plugin_definition = $media_source->getPluginDefinition();
+
+ $this->thumbnail->alt = '';
if (!empty($plugin_definition['thumbnail_alt_metadata_attribute'])) {
$this->thumbnail->alt = $media_source->getMetadata($this, $plugin_definition['thumbnail_alt_metadata_attribute']);
}
- else {
- $this->thumbnail->alt = $this->t('Thumbnail', [], ['langcode' => $this->langcode->value]);
- }
-
- // Set the thumbnail title.
- if (!empty($plugin_definition['thumbnail_title_metadata_attribute'])) {
- $this->thumbnail->title = $media_source->getMetadata($this, $plugin_definition['thumbnail_title_metadata_attribute']);
- }
- else {
- $this->thumbnail->title = $this->label();
- }
return $this;
}
diff --git a/core/modules/media/src/Plugin/media/Source/Image.php b/core/modules/media/src/Plugin/media/Source/Image.php
index a83a514..34b565c 100644
--- a/core/modules/media/src/Plugin/media/Source/Image.php
+++ b/core/modules/media/src/Plugin/media/Source/Image.php
@@ -22,7 +22,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* label = @Translation("Image"),
* description = @Translation("Use local images for reusable media."),
* allowed_field_types = {"image"},
- * default_thumbnail_filename = "no-thumbnail.png"
+ * default_thumbnail_filename = "no-thumbnail.png",
+ * thumbnail_alt_metadata_attribute = "thumbnail_alt_value"
* )
*/
class Image extends File {
@@ -138,6 +139,9 @@ class Image extends File {
case 'thumbnail_uri':
return $uri;
+
+ case 'thumbnail_alt_value':
+ return $media->get($this->configuration['source_field'])->alt ?: parent::getMetadata($media, $name);
}
return parent::getMetadata($media, $name);
diff --git a/core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php b/core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php
index 85e4e5d..de352e0 100644
--- a/core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php
+++ b/core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php
@@ -167,13 +167,13 @@ abstract class MediaResourceTestBase extends EntityResourceTestBase {
],
'thumbnail' => [
[
- 'alt' => 'Thumbnail',
+ 'alt' => '',
'width' => 180,
'height' => 180,
'target_id' => (int) $thumbnail->id(),
'target_type' => 'file',
'target_uuid' => $thumbnail->uuid(),
- 'title' => 'Llama',
+ 'title' => NULL,
'url' => $thumbnail->url(),
],
],
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
index faf58c1..cb01380 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
@@ -59,6 +59,8 @@ class MediaSourceImageTest extends MediaSourceTestBase {
// Make sure the thumbnail is displayed from uploaded image.
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'example_1.jpeg');
+ // Ensure the thumbnail has the correct alt attribute.
+ $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'alt', 'Image Alt Text 1');
// Load the media and check that all fields are properly populated.
$media = Media::load(1);
diff --git a/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php
index 79cb7b4..ecf1778 100644
--- a/core/modules/media/tests/src/Kernel/MediaSourceTest.php
+++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php
@@ -40,7 +40,6 @@ class MediaSourceTest extends MediaKernelTestBase {
'value' => 'Snowball',
],
'thumbnail_uri' => [
- 'title' => 'Thumbnail',
'value' => 'public://TheSisko.png',
],
]);
@@ -230,7 +229,7 @@ class MediaSourceTest extends MediaKernelTestBase {
// Save a media item and make sure thumbnail was added.
\Drupal::state()->set('media_source_test_attributes', [
- 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
+ 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
]);
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
@@ -242,45 +241,46 @@ class MediaSourceTest extends MediaKernelTestBase {
$this->assertSame('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media->save();
$this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media item.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ // We expect the title not to be present on the Thumbnail.
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
// Now change the metadata attribute and make sure that the thumbnail stays
// the same.
\Drupal::state()->set('media_source_test_attributes', [
- 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail2.jpg'],
+ 'thumbnail_uri' => ['value' => 'public://thumbnail2.jpg'],
]);
$this->assertSame('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media->save();
$this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not preserved.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
// Remove the thumbnail and make sure that it is auto-updated on save.
$media->thumbnail->target_id = NULL;
$this->assertSame('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media->save();
$this->assertSame('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
// 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'],
+ 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
]);
$media->field_media_test->value = 'some_new_value';
$this->assertSame('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media->save();
$this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
// Change the thumbnail metadata attribute and make sure that the thumbnail
// is set correctly.
\Drupal::state()->set('media_source_test_attributes', [
- 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
- 'alternative_thumbnail_uri' => ['title' => 'Should be used', 'value' => 'public://thumbnail2.jpg'],
+ 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
+ 'alternative_thumbnail_uri' => ['value' => 'public://thumbnail2.jpg'],
]);
\Drupal::state()->set('media_source_test_definition', ['thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri']);
$media = Media::create([
@@ -293,14 +293,14 @@ class MediaSourceTest extends MediaKernelTestBase {
$this->assertSame('public://thumbnail2.jpg', $media_source->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
$media->save();
$this->assertSame('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
// Enable queued thumbnails and make sure that the entity gets the default
// thumbnail initially.
\Drupal::state()->set('media_source_test_definition', []);
\Drupal::state()->set('media_source_test_attributes', [
- 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
+ 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
]);
$this->testMediaType->setQueueThumbnailDownloadsStatus(TRUE)->save();
$media = Media::create([
@@ -311,8 +311,8 @@ class MediaSourceTest extends MediaKernelTestBase {
$this->assertSame('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
$media->save();
$this->assertSame('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
// Process the queue item and make sure that the thumbnail was updated too.
$queue_name = 'media_entity_thumbnail';
@@ -330,18 +330,15 @@ class MediaSourceTest extends MediaKernelTestBase {
$media = Media::load($media->id());
$this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
- $this->assertSame('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertSame('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('', $media->thumbnail->alt);
- // Set alt and title metadata attributes and make sure they are used for the
- // thumbnail.
+ // Set the alt metadata attribute and make sure it's used for the thumbnail.
\Drupal::state()->set('media_source_test_definition', [
'thumbnail_alt_metadata_attribute' => 'alt',
- 'thumbnail_title_metadata_attribute' => 'title',
]);
\Drupal::state()->set('media_source_test_attributes', [
- 'alt' => ['title' => 'Alt text', 'value' => 'This will be alt.'],
- 'title' => ['title' => 'Title text', 'value' => 'This will be title.'],
+ 'alt' => ['value' => 'This will be alt.'],
]);
$media = Media::create([
'bundle' => $this->testMediaType->id(),
@@ -350,8 +347,8 @@ class MediaSourceTest extends MediaKernelTestBase {
]);
$media->save();
$this->assertSame('Boxer', $media->getName(), 'Correct name was not set on the media item.');
- $this->assertSame('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
- $this->assertSame('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ $this->assertEmpty($media->thumbnail->title);
+ $this->assertSame('This will be alt.', $media->thumbnail->alt);
}
/**
diff --git a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php
index 33e0197..cf5d3da 100644
--- a/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php
+++ b/core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php
@@ -371,7 +371,7 @@ class ContentEntityTest extends KernelTestBase {
$values = $media_source->current()->getSource();
$this->assertEquals(1, $values['mid']);
$this->assertEquals('Foo media', $values['name'][0]['value']);
- $this->assertEquals('Foo media', $values['thumbnail'][0]['title']);
+ $this->assertNull($values['thumbnail'][0]['title']);
$this->assertEquals(1, $values['uid'][0]['target_id']);
$this->assertEquals('image', $values['bundle'][0]['target_id']);
}