Enable media fields in custom captions

Created on 7 January 2019, almost 6 years ago
Updated 4 April 2022, over 2 years ago

Problem/Motivation

When using PhotoSwipe on a media reference field, only the parent node and the file entity is available for token replacement in captions. Currently the fields of the actual media entity cannot be used for captions.

Proposed resolution

If available, add the current media entity to the scope when replacing tokens in captions.

Remaining tasks

Review needed.

Feature request
Status

Fixed

Version

3.0

Component

Code

Created by

🇩🇪Germany rob_pr

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.

  • 🇨🇦Canada peterhebert

    I am trying to use a field attached to the media entity 'field_caption' as the caption source, but when I chhose the Photoswipe Responsive field formatter on my node, the media field doesn't appear as a source, and I cannot figure out the correct token. I have tried:

    [node:field_media:entity:field_caption] - repeats the caption from the first image delta on all images... no token for non-specific delta?
    [media:field_caption] - nothing

    Is this possible? If so, can you document how to do this via a token, or using a hook?

  • 🇨🇭Switzerland handkerchief

    @peterhebert I had the same issue, I was able to solve this with the peprocess hook:

    function HOOK_preprocess_photoswipe_image_formatter(&$variables) {
      // Get correct caption for the image.
      $imageDTO = ImageDTO::createFromVariables($variables);
      $media = $imageDTO->getItem()->getParent()->getEntity();
      $media_title = $media->label();
      $example_text = $media->field_example_text->value;
    
      $final_caption = $media_title . '<br>' . $example_text;
      $variables['attributes']['data-overlay-title'] = $final_caption;
    }
    

    The code is just an example. Hope this helps someone.

Production build 0.71.5 2024