Problem/Motivation

Our issue is similar to this comment: https://www.drupal.org/project/photoswipe/issues/3024243#comment-15332309

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.

Issue fork photoswipe-3453071

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pyrello created an issue. See original summary.

vlad.dancer’s picture

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;

vlad.dancer’s picture

Status: Active » Needs review
vlad.dancer’s picture

Component: Documentation » Code
anybody’s picture

Category: Support request » Feature request
Status: Needs review » Needs work

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?

handkerchief’s picture

thomas.frobieter’s picture

I encountered a similar issue. We also have a separate 'field_caption' field on 'Media Image', and this field is n/a in the 'PhotoSwipe-Caption' dropdown menu. Using the token [media:field_caption] does not work either.

The formatter is Photoswipe Responsive.

anybody’s picture

Assigned: Unassigned » grevil

@grevil: Could you please have a look in the next days?

grevil’s picture

Title: Dynamic caption: How to use a dedicated caption field on a media entity? » Allow to use referenced entity fields as a caption source

IMO, this issue shouldn't be about improving the token compatibility, but rather be a feature request to add compatibility to use the referenced entity fields as a caption source.

grevil changed the visibility of the branch 3453071-dynamic-caption-better-custom-tokens to hidden.

grevil’s picture

Assigned: grevil » Unassigned
Status: Needs work » Needs review

I also added a test. Please review!

anybody’s picture

@grevil LGTM, but as the original request asked for a token reference support, we should either add that here too (using a Drupal typical token name like in similar reference cases) or at least add a follow-up.

anybody’s picture

Status: Needs review » Reviewed & tested by the community

RTBC'd by thomas.frobieter. Code-wise this is fine for me. Please note #14! (Follow-up for reference token)

  • grevil committed 51909aa3 on 5.x
    [#3453071] feat: Allow to use referenced entity fields as a caption...
grevil’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

grevil’s picture

@grevil LGTM, but as the original request asked for a token reference support

Not quite. The original request was, whether there is a way to use a field on the referenced entity (in this case media) as caption source. Their approach was, to use tokens, we simply provided a different approach to fix this issue!

Nevertheless, I created a follow-up issue here: #3548372: Photoswipe Caption: Allow to use referenced entity's tokens as caption source.

anybody’s picture

Thx, yes the token approach would also be very nice and flexible, but should be implemented by someone who needs it. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.