Is there a way to use content from another view field as a photoswipe caption. Right now only Image Title, Image Alt and Node Title are available... I'm looking to include something more custom, generated with fields from that particular view.

Thanks!

Comments

ChuckSimply created an issue. See original summary.

chucksimply’s picture

Issue summary: View changes
Kunstwirt’s picture

Did you find a solution in the meantime? We are looking for a similar thing, but mainly for a way to display captions next to the thumbnails (maybe we disabled something by accident?). Thanks, Frank

see also my request: https://www.drupal.org/project/photoswipe/issues/2984783

anybody’s picture

I think paid development might be an option here, if you can't develop a patch inhouse. Perhaps someone is interested?

chucksimply’s picture

If anyone wants to provide an estimate to develop this feature, maybe we can chip in to get it done. This really is a huge missing piece, and would push this module to the next level.

marassa’s picture

I added an option of a custom caption with tokens, just like in the Colorbox module. It's actually more flexible than using one views field as you can combine more than one field and add your own markup if needed. If you only want one field, just use one token, like [node:body].
There seems to be some work in progress in 8.x-2.x-dev that kind of breaks some of functionality working in the 8.x-2.2 release. So I am going to post two patches: one will apply to 8.x-2.x-dev (to be hopefully committed one day) and the other will apply to 8.x-2.2 - for those who run the release version and need this right now. Please review and test.

marassa’s picture

Status: Active » Needs review

The last submitted patch, 6: custom_caption_with_tokens-2960249-6.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 6: custom_caption_for_8.x-2.2.patch, failed testing. View results

marassa’s picture

StatusFileSize
new4.39 KB
new3.86 KB

Re-posting the same patches to be tested against correct version (and not tested at all in case of the 8.x-2.2 patch). Could not figure out how to change that without re-posting.

marassa’s picture

Status: Needs work » Needs review

The last submitted patch, 10: custom_caption_with_tokens-2960249-6.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marassa’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
marassa’s picture

StatusFileSize
new4.45 KB
new3.92 KB

Added a couple of enhancements:
- a text field is way too small for a good fat caption using lots of tokens; changed to textarea
- made sure the token replacer respects the current page language

The last submitted patch, 14: custom_caption_with_tokens-2960249-14.patch, failed testing. View results

chucksimply’s picture

Awesome... will test out this week and report back. Thank you for this work!

chucksimply’s picture

Tested the 8.2.2 release patch... works beautifully. The token solution is by far the most flexible + versatile. Thank you again... I feel like this was a subtle but pretty major missing piece.

anybody’s picture

Thank you very much, should a token browser link (AJAX) be added perhaps? Eventually in the field description?

Two more questions:

1. Where does the output filtering and token replacement happen? I couldn't find that in the patch. It's also important for security reasons.

2. Can you please explain this change? Isn't that supposed to break other things?

-  $entity = $item->getParent()->getValue();
+  $entity = $item->getParent()->getParent()->getValue();
marassa’s picture

@Anybody:

should a token browser link (AJAX) be added perhaps? Eventually in the field description?

I'm not sure I understand the question - do you mean the list of available tokens? It's right there, under the Custom caption field.

Where does the output filtering and token replacement happen?

In photoswipe.theme.inc:

+      case 'custom':
+        $entity_type = $entity->getEntityTypeId();
+        $caption = \Drupal::token()->replace($settings['photoswipe_caption_custom'],
+          [$entity_type => $entity, 'file' => $item],
+          ['clear' => TRUE, 'langcode' => \Drupal::languageManager()->getCurrentLanguage()->getId()]);
+        break;
+
       default:
         // Assume the user wants to use another node field as the caption.
Can you please explain this change?

Sure: I initially based my code on 8.x-2.2 where the entity is obtained by the double getParent() call. It works for me (and apparently for the other 1207 people who use the module with Drupal 8). When I tried to create a "proper" patch against dev, I noticed that the double getParent() call has been replaced by a single getParent() there which is probably very good for the Media use case but totally breaks everything when you work with simple image fields in simple nodes.
The change in dev was introduced by https://www.drupal.org/project/photoswipe/issues/2977943 - I linked it to this issue and made a few comments in that issue, including a proposed patch that does away with all this getParent() monkey business. My patch and whatever is done under that issue should be somehow merged together so that the module works with both Media fields and simple image fields.

anybody’s picture

Thank you very very much for your clarification, @ndlarge!
1) Sorry then it's my mistake, I couldn't find that piece in the code.

2) Ok... yes I'm also quite unhappy with the current state... would you like to help me clean up and become co-maintainer of this module? I was also only a "helper" in fixing several problems. That would be great!
Of course we should ensure everything works as expected in all cases... What about writing tests in the next step? Meta-issue? Would you be interested?

marassa’s picture

@Anybody:
I am flattered by your co-maintainer proposal but I'm afraid I'm not qualified/experienced enough to maintain a popular module like this (for example I have no idea how to write tests, I am not too fluent in git etc etc etc) and last but not least I don't have the time it would take. I simply wanted Photoswipe for the only site I am developing for myself, found a few shortcomings, did some copying and pasting, was satisfied with the result and decided to share it with the community. I am more than happy to share what I developed and what works for me but I am hardly able to clean up other people's code dealing with stuff (like Media) I have no clue about.
Maybe @Niklan would be interested in reconciling media and non-media use cases? He is much more experienced than me.

anybody’s picture

Thank you ndlarge for your honesty, we'd really appreciate some more help. Then back to topic!

anybody’s picture

Retest against latest dev after #2977943: Support Media and Entity References was committed to dev.

The last submitted patch, 14: custom_caption_with_tokens-2960249-14.patch, failed testing. View results

anybody’s picture

Status: Needs review » Needs work

The patch needs a reroll against the latest dev now. Thanks!

marassa’s picture

StatusFileSize
new3.92 KB

Patch rerolled against the latest dev. Works for me with image fields in entity display and views contexts, didn't test with Media!

marassa’s picture

Status: Needs work » Needs review
marassa’s picture

Component: Miscellaneous » Code

Status: Needs review » Needs work

The last submitted patch, 26: custom_caption_with_tokens-2960249-26.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Anybody committed 4cc38db on 8.x-2.x
    Issue #2960249 by ndlarge, Anybody, ChuckSimply: Custom caption based on...
anybody’s picture

Status: Needs work » Fixed

Thank you all. This is now in the latest 8.x-2.x dev release. Please test!

Status: Fixed » Closed (fixed)

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