Using a media entity reference field in combination with the "Photoswipe" display option throws an error

Created on 23 March 2022, over 2 years ago
Updated 8 August 2023, over 1 year ago

Problem/Motivation

Using a media entity reference field on an entity, setting the display option for the entity reference field to "Photoswipe" and finally creating an instance of the entity, will lead to the following error:

The website encountered an unexpected error. Please try again later.
Error: Call to undefined method Drupal\media\Entity\Media::getFileUri() in Drupal\photoswipe\ImageDTO->__construct() (line 108 of modules/custom/photoswipe/src/ImageDTO.php).

Upon further investigations, I found out, that we have two problems in the "ImageDTO.php" construct method:

The obvious one is, that Media does not have the method getFileUri(), so we need to add a condition which checks if we are currently working with a file or a media object. We already implemented this for the $item variable in line 101-103:

    $this->item = (($item = $variables['item']) && ($item->entity instanceof MediaInterface)) && $item->entity->hasField($this->settings['photoswipe_reference_image_field'])
      ? $item->entity->get($this->settings['photoswipe_reference_image_field'])
      : $item;

But, and this is problem number 2, this does not actually work properly, since we always have "$this->item" assigned to "$item" regardless if it is a media or a file entity.

At first, I thought, this was related to #3270757: Remove media_entity code from >=3.1 , but it also doesn't work in the current 3.x stable Version nor the older stable 3.0.2 Version!

🐛 Bug report
Status

Fixed

Version

5.0

Component

Code

Created by

🇩🇪Germany Grevil

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024