Problem/Motivation

Now that the media module is in core, I think it would be helpful to support that in addition to the image field.

Proposed resolution

I put together a quick patch that simply adds another formatter for entity_reference field types that point to media items. It's mostly a copy of the original for formatter with some slight changes to support reading from the media item's thumbnail.

Comments

jacobbell84 created an issue. See original summary.

aimevp’s picture

StatusFileSize
new6.37 KB

I have been using this module with this patch and noticed that when I select a newly created media item the image style of the selected image is not generated. Attached is a new patch that adds my fix to the original patch.
The patch also cleans and simplifies the code a bit of the new media formatter.

I have not yet tested if the same problem occurs when using the original "image" formatter but I'm guessing the "createDerivative" function could be of use there as well. But that may be better suited in a new issue.

nelslynn’s picture

Thanks for the patches. The image path is not working (Public://IMAGE_FOLDER/IMG_FILE_NAME). I had to modify the background style in the twig template to this:

<div class="{{ background_image_selector }}" style="background-image: url('{{file_url(image_uri)}}');"></div>

I know there's probably another solution, but adding "file_uri()" gives the full file path. This works for the inline style version only.

This happens using ddev as a development platform. Am I missing something?

jacobbell84’s picture

StatusFileSize
new6.46 KB

@nelslynn, looks like there was a bug with using the original image vs an image style. This latest patch should fix that issue.

zulljin’s picture

Hey,

I'll review patch and apply after testing.

jacobbell84’s picture

ConradFlashback’s picture

#4 works well for me, please commit (after Christmas obviously :) )

bramvandenbulcke’s picture

I'm looking for a module to support background images with media entities. There are different modules supporting background images but none of them currently support media entities (only regular image fields).

I was really happy to see this patch. I was testing on a Drupal 9 install and applied the patch to this modules, version 1.7.

It's not working for me. This is the output:

Error: Call to undefined method Drupal\file\Entity\File::url() in /Users/Bram/Sites/drupal9-test/modules/contrib/background_image_formatter/src/Plugin/Field/FieldFormatter/BackgroundMediaImageFormatter.php on line 130 #0 /Users/Bram/Sites/drupal9-test/core/lib/Drupal/Core/Field/FormatterBase.php(89): Drupal\background_image_formatter\Plugin\Field\FieldFormatter\BackgroundMediaImageFormatter->viewElements(Object(Drupal\Core\Field\EntityReferenceFieldItemList), 'en')

ConradFlashback’s picture

@Zulljin Any news about the #4 test and commit?
Thanks

msabares’s picture

StatusFileSize
new6.45 KB
new570 bytes

This patched replaces the deprecated function of url() with createFileUrl() for Drupal 9

ConradFlashback’s picture

10 works thanks.

zulljin’s picture

Assigned: Unassigned » zulljin
Status: Needs review » Reviewed & tested by the community

Hi, Great news, I was test it. Patch #10 works fine.

  • Zulljin committed d452ab26 on 2.0.x
    Issue #3069852 by jacobbell84, msabares, aimevp, ConradFlashback,...
zulljin’s picture

Status: Reviewed & tested by the community » Fixed

Fixed in 2.0.3.

Status: Fixed » Closed (fixed)

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