Language support

Created on 7 May 2025, 9 days ago

Problem/Motivation

On a multilingual site, for example using English and Spanish languages, when using media image fields the translated media values (e.g alt and title) aren't being used but instead the default language version is always being used.

Proposed resolution

To resolve in the file: /src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php I changed the code as shown below.

from (Photoswipe v4.0.1):

      // Check if the entity is an Entity (e.g. wasn't deleted) and the entity
      // is accessible:
      if ($item->entity instanceof EntityInterface && $item->entity->access('view')) {
        $elements[$delta] = [
          '#theme' => 'photoswipe_image_formatter',
          '#item' => $item,
          '#entity' => $items->getEntity(),
          '#display_settings' => $settings,
          '#delta' => $delta,
        ];
      }

to:

      // Check if the entity is an Entity (e.g. wasn't deleted) and the entity
      // is accessible:
      if ($item->entity instanceof EntityInterface && $item->entity->access('view')) {
        // Get the translated version of the referenced entity.
        $translated_entity = $this->entityRepository->getTranslationFromContext($item->entity, $langcode);

        // Use translated media entity for the item.
        $item->entity = $translated_entity;

        $elements[$delta] = [
          '#theme' => 'photoswipe_image_formatter',
          '#item' => $item,
          '#entity' => $items->getEntity(),
          '#display_settings' => $settings,
          '#delta' => $delta,
        ];
      }

Which I found resolved the issue.

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇬🇧United Kingdom mikee

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

Comments & Activities

Production build 0.71.5 2024