Dynamic caption: How to use a dedicated caption field on a media entity?

Created on 6 June 2024, 7 months ago
Updated 20 August 2024, 4 months ago

Problem/Motivation

Our issue is similar to this comment: https://www.drupal.org/project/photoswipe/issues/3024243#comment-15332309 ✨ Enable media fields in custom captions Fixed

We have an article content type with a multi-value media entity reference field that connects to an image media entity type. The image media entity has a dedicated caption field, which we would like to use as our PhotoSwipe caption.

We're using the custom token setting and we tried using the [media:field_media_caption] token, which did not work. Looking through the token browser, we cannot find a clear way to connect to the field. We are looking for some guidance on how this could be accomplished.

✨ Feature request
Status

Needs work

Version

5.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States pyrello

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

Merge Requests

Comments & Activities

  • Issue created by @pyrello
  • πŸ‡ΊπŸ‡¦Ukraine vlad.dancer Kyiv

    Right now you can't use media tokens when using photoswipe field formatter on node for media entity reference field. That's because this code $entity_type = $imageDTO->getEntity()->getEntityTypeId();(source) returns a host entity (node) when formatter being called from node rendering.
    I have a similar situation but we are using paragraph for storing image with additional data. We're also using custom caption field but inside paragraph. I modified https://git.drupalcode.org/project/photoswipe/-/blob/5.x/modules/photosw... by this:

    case 'custom':
            $entity_type = $imageDTO->getEntity()->getEntityTypeId();
            $entity = $imageDTO->getEntity();
            $token = $thirdPartySettings['photoswipe_dynamic_caption']['photoswipe_caption_custom'];
    
            // Try to extract parent entity type from token.
            $token_parts = explode(':', str_replace(['[', ']'], '', $token));
            $token_entity_type = $token_parts[0] ?? NULL;
            // Let's use parent entity for token generation.
            if (count($token_parts) > 1) {
              $entity_type = $token_entity_type;
              $entity = $imageDTO->getItem()->getParent()->getEntity();
            }
    
            $caption = \Drupal::service('token')->replace($token,
              [
                $entity_type => $entity,
                'file' => $imageDTO->getItem(),
              ],
              [
                'clear' => TRUE,
                'langcode' => \Drupal::service('language_manager')->getCurrentLanguage()->getId(),
              ]
            );
            break;
    
  • Pipeline finished with Success
    6 months ago
    Total: 247s
    #203790
  • Status changed to Needs review 6 months ago
  • πŸ‡ΊπŸ‡¦Ukraine vlad.dancer Kyiv
  • πŸ‡ΊπŸ‡¦Ukraine vlad.dancer Kyiv
  • Status changed to Needs work 4 months ago
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    I agree it would be helpful to add related / parent entity tokens, but I dislike this approach:

            // Try to extract parent entity type from token.
            $token_parts = explode(':', str_replace(['[', ']'], '', $token));
            $token_entity_type = $token_parts[0] ?? NULL;
    

    Any other ideas how to add the references token more cleanly? Has this perhaps already been solved somewhere else?

  • πŸ‡¨πŸ‡­Switzerland handkerchief

    @anybody see here: https://www.drupal.org/project/photoswipe/issues/3024243#comment-15804453 ✨ Enable media fields in custom captions Fixed

Production build 0.71.5 2024