I try to use PhotoSwipe formatter with media in views and get the error from the original - there's a bunch more in my error but that is the first line.

Error: Call to undefined method Drupal\media\Entity\Media::getFileUri() в template_preprocess_photoswipe_image_formatter()

Is there any solution to this?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

itcrowd72 created an issue. See original summary.

ergophobe’s picture

Title: PhotoSwipe doesn't work with media in views » Fatal Error with Media in Views: getFileUri is undefined
Version: 8.x-2.x-dev » 8.x-2.9
Priority: Normal » Major

I think this meets the criteria for a major bug since Photoswipe is unusable with Media and Views, which is part of the Photoswipe functionality.

Doing some digging, but not close to figuring this out. The error message is the same as in the original issue report.

Error: Call to undefined method Drupal\media\Entity\Media::getFileUri() in template_preprocess_photoswipe_image_formatter() (line 30 of modules\contrib\photoswipe\photoswipe.theme.inc).

Note that the line number is not the same as in the distro, because I have some debug code in there.

Setting some breakpoints and examining code execution, there are a number of things that fail prior to that.

  if (($item->entity instanceof Drupal\media\MediaInterface || $item->entity instanceof Drupal\media_entity\MediaInterface) && $item->entity->hasField($settings['photoswipe_reference_image_field'])) {

When I inspect $item->entity the type is Drupal\media\Entity\Media, so both parts of the OR statement fail.

Also, $settings['photoswipe_reference_image_field'] is an empty string. So that is not getting passed, the the last part of the conditional fails as well.

The other settings for image styles and captions are fine.

viappidu’s picture

I don't use photoswipe in views though the patch I created in the related layout builder issue solved my problems. Please check if this works also for this issue

viappidu’s picture

Status: Active » Needs review

Patch added to the related issue at #4 please review

ivnish’s picture

Patch doesn't solve the problem.

ivnish’s picture

del

chucksimply’s picture

Same issue and error here.... and patch from #4 doesn't work. @ivnish, did you find a solution or work around?

chucksimply’s picture

Status: Needs review » Needs work
Burlyn84’s picture

Issue summary: View changes

I am facing the exact same error.

calebyoder’s picture

Yes, we have this site that says "The website encountered an unexpected error. Please try again later." at the bottom of the pages and when I try to run cron it shows a blank white page with "The website encountered an unexpected error. Please try again later."

In the error log I am getting this:

Error: Call to undefined method Drupal\taxonomy\Entity\Term::getFileUri() in template_preprocess_photoswipe_image_formatter() (line 30 of /home/PLACEHOLDER/public_html/modules/contrib/photoswipe/photoswipe.theme.inc)

I tried implementing the patch referred to above in comment #3 (https://www.drupal.org/project/photoswipe/issues/3040117#comment-13226403) but doesn't seem to fix it.

Anyone have any idea what the issue is and how to fix?

Thanks!

chucksimply’s picture

Looks to be related to this issue (Support for Media and Entity References) which is deemed as fixed. But based on my testing in Drupal 8.8, it should be reopened. See my feedback here. Hopefully, we can get some progress on one of these threads.

chucksimply’s picture

Status: Needs work » Active
No Sssweat’s picture

Seems like the fix has not been committed to the stable version.

Please try with the 2.x-dev version version, as this other comment indicates and let us know how it goes.

Burlyn84’s picture

Do you really mean that? According to the module page that was released in 2018.

If that is what it takes, we can do that, but seems kind of strange to have to go back that far to get a working version.

chucksimply’s picture

Tried 2.x-dev with Drupal 8.8. Bug still exists. I believe the issue is that the "image field of the referenced entity" field doesn't contain the media image field as it should (In a View when selecting the photoswipe formatter... see attached screenshot). Then saving without a media image field selected and viewing that views page throws this above error?

chucksimply’s picture

Okay, found a temporary workaround. As the original issue shows, the "image field of the referenced entity" doesn't have a value, and when saved without a value selected, the above error shows.

The problem is rooted on line 161 of /src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php...

$target_bundles = $this->fieldDefinition->getSetting('handler_settings')['target_bundles'];

The handler_settings array is empty within a view.. so $target_bundles is null.

If you include...

if(empty($target_bundles)){
      $target_bundles = [
        'image' => 'image'
      ];
    }

... directly after line 161 (replacing "image" with your media bundle) the "image field of the referenced entity" will populate correctly. I know this is a module hack and not a long term solution, but hopefully, someone else can take this info and create a more dynamic solution and patch.

extect’s picture

Instead of including the media reference field in your view, try to setup a relationship to the media entity and then include the image field directly to make photoswipe work.

chucksimply’s picture

Thanks @extect, that's a work around, but is limiting for many reasons. It's still true that this is a bug, and deserves an actual code fix.

Thib’s picture

The tip of @extect is a good alternative that I use

Anybody’s picture

Status: Active » Closed (outdated)

Cleanup: Closing outdated 8.x-2.x branch issues.

Please try with the latest 3.x version if the issue persists. If yes, reopen the issue and switch the issue version to 3.x with a clear description how to reproduce the issue in 3.x

Thank you! :)