Unverified Commit e6dfd046 authored by alexpott's avatar alexpott

Issue #2956368 by marcoscano, Nitebreed, ph53: MediaThumbnailFormatter...

Issue #2956368 by marcoscano, Nitebreed, ph53: MediaThumbnailFormatter produces unhelpful text alternative and title attributes for media thumbnails
parent ae6c7444
......@@ -159,20 +159,11 @@ protected function updateThumbnail($from_queue = FALSE) {
// 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;
}
......
......@@ -22,7 +22,8 @@
* 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 @@ public function getMetadata(MediaInterface $media, $name) {
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);
......
......@@ -167,13 +167,13 @@ protected function getExpectedNormalizedEntity() {
],
'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(),
],
],
......
......@@ -59,6 +59,8 @@ public function testMediaImageSource() {
// 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);
......
......@@ -40,7 +40,6 @@ public function testSave() {
'value' => 'Snowball',
],
'thumbnail_uri' => [
'title' => 'Thumbnail',
'value' => 'public://TheSisko.png',
],
]);
......@@ -230,7 +229,7 @@ public function testThumbnail() {
// 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 @@ public function testThumbnail() {
$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 @@ public function testThumbnail() {
$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 @@ public function testThumbnail() {
$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 @@ public function testThumbnail() {
$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 @@ public function testThumbnail() {
]);
$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);
}
/**
......
......@@ -371,7 +371,7 @@ public function testMediaSource() {
$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']);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment