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
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
Comment #2
vlad.dancerRight 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:
Comment #4
vlad.dancerComment #5
vlad.dancerComment #6
anybodyI agree it would be helpful to add related / parent entity tokens, but I dislike this approach:
Any other ideas how to add the references token more cleanly? Has this perhaps already been solved somewhere else?
Comment #7
handkerchief@anybody see here: https://www.drupal.org/project/photoswipe/issues/3024243#comment-15804453
Comment #8
thomas.frobieterI 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.
Comment #9
anybody@grevil: Could you please have a look in the next days?
Comment #10
grevil commentedIMO, 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.
Comment #13
grevil commentedI also added a test. Please review!
Comment #14
anybody@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.
Comment #15
anybodyRTBC'd by thomas.frobieter. Code-wise this is fine for me. Please note #14! (Follow-up for reference token)
Comment #17
grevil commentedComment #19
grevil commentedNot 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.
Comment #20
anybodyThx, yes the token approach would also be very nice and flexible, but should be implemented by someone who needs it. Thanks!