Problem/Motivation

Followup to comment #123 on #2825215-123: Media initiative: Roadmap.

Media Image reference fields are rendered as small thumbnails by default. This is not the behavior of traditional image fields.
Default media image display

Proposed resolution

Change the Media Image default display mode to use the original image (currently uses Medium - 220x220).

Here's a video demonstrating the behavior:
https://www.youtube.com/watch?v=9t23kO8olWs

Comments

balsama created an issue. See original summary.

balsama’s picture

Issue summary: View changes
balsama’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new2.95 MB
new630 bytes

Here's a patch that just sets the default view mode to orginal image. So following the same steps as in the video will now result in this:

marcoscano’s picture

I believe we should adjust \Drupal\Tests\media\FunctionalJavascript\MediaDisplayTest::testMediaDisplay() as well, but let's see what the testbot says

Status: Needs review » Needs work

The last submitted patch, 3: 2934850-3.patch, failed testing. View results

balsama’s picture

Status: Needs work » Needs review
StatusFileSize
new2.28 KB

Updated the patch to include changes to MediaDisplayTest. I'm not sure how to assert that it's the original image though (they used to assert img.image-style-medium - original images don't get any classes added). We could assert that there are no classes, but that seems brittle.

marcoscano’s picture

Looks good to me, just a minor remark:

+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
@@ -163,8 +161,8 @@ public function testMediaDisplay() {
     // Only one element is present inside the media container.
     $this->assertEquals(1, count($page->findAll('css', '.field--name-field-related-media article.media--type-image > div')));
-    // Assert the image is present, with "medium" image style.
-    $assert_session->elementExists('css', '.field--name-field-related-media article.media--type-image img.image-style-medium');
+    // Assert the image is present.
+    $assert_session->elementExists('css', '.field--name-field-related-media article.media--type-image');
   }

If we are not checking the img tag, then this last assert is redundant to the previous one. Maybe we can assert that .field--name-field-related-media article.media--type-image img exists, without specifying any class?

phenaproxima’s picture

Status: Needs review » Needs work
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
@@ -81,10 +81,8 @@ public function testMediaDisplay() {
-    $assert_session->elementExists('css', 'img.image-style-medium', $media_item);

We shouldn't remove this line; just change it to $assert_session->elementExists('css', 'img', $media_item);

Also, we could assert the original image (albeit not perfectly), by getting the 'class' attribute, splitting it by whitespace, and asserting that none of the classes start with image-style-.

berdir’s picture

Makes sense, although it is IMHO a bad idea for an actual site to ever use original image, you never know what your users are uploading, might be huge 5MB pictures. But lets make media and normal image fields consistent and then we can think about adding something like a HD image style that would be used by default to standard?

About the test, we might also be able to assert the actual src attribute of the image?

In paragraphs test, we have something like this, except we'd want to use a getAtribute() on the image element that we load with elementExists():

    $img1_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage1.jpg'));
    $img2_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage2.jpg'));

    // Check the text and image after publish.
    $this->assertText('Test text 1');
    $this->assertRaw('<img src="' . file_url_transform_relative($img1_url));
balsama’s picture

Status: Needs work » Needs review
StatusFileSize
new2.74 KB
new1.58 KB

Added the assertion that the image exists back in (comment #8) and assert that the actual src attribute doesn't point to an image derivative (comment #9).

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Media Initiative

I think this looks right.

  • xjm committed 319f023 on 8.5.x
    Issue #2934850 by balsama, Berdir: Media Images should be rendered at a...
xjm’s picture

Ah, much better!

I tested this manually and confirmed Media image fields now behave the same way as image fields do by default.

+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
@@ -81,10 +81,13 @@ public function testMediaDisplay() {
+    $expected_image_src = file_url_transform_relative(file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/example_1.jpeg')));

I was surprised by file_url_transform_relative() here, but I couldn't come up with any specific concern. So seems okay.

Committed to 8.5.x. Thanks!

xjm’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.