Hey, thank's for this awesome module.

There is a minor issue where the html is printed different when there is one image and when there is multiple images.

When there is one image the root div is the field name and the inner div is photoswipe-gallery;

<div class="field field--name-field-myfield field--type-image field--label-hidden field__items">
        <div class="field__item">
                <span class="photoswipe-gallery photoswipe-gallery--fallback-wrapper" data-once="photoswipe">
                        <a href="http://local.blabla/sites/default/files/styles/original/public/2023-09/gen9A8D.tmp.jpg.webp?itok=3ZPTtNXj" class="photoswipe" data-pswp-width="438" data-pswp-height="399" data-once="photoswipePrepareGalleries">
                                <img src="/sites/default/files/styles/landscape/public/2023-09/gen9A8D.tmp.jpg.webp?itok=v0o7ki-4" width="1000" height="560" alt="Eum mauris tation." title="Cogo loquor utinam." loading="lazy" class="image-style-landscape">
                        </a>
                </span>
        </div>
</div>

But when there are multiple images the html structure changes and the parent becomes photoswipe-gallery and field name becomes the child div.

<div class="photoswipe-gallery" data-once="photoswipe">
        <div class="field field--name-field-myfield field--type-image field--label-hidden field__items">
                <div class="field__item">
                        <a href="http://local.blabla/sites/default/files/styles/original/public/2023-09/gen9A3C.tmp.png.webp?itok=5U5qcFjR" class="photoswipe" data-pswp-width="364" data-pswp-height="553" data-once="photoswipePrepareGalleries"><img src="/sites/default/files/styles/landscape/public/2023-09/gen9A3C.tmp.png.webp?itok=VSyZkiPH" width="1000" height="560" alt="Facilisis humo pne" title="Abico diam incassum " loading="lazy" class="image-style-landscape"></a>
                </div>
                <div class="field__item">
                        <a href="http://local.blabla/sites/default/files/styles/original/public/2023-09/gen9B6C.tmp.jpeg.webp?itok=vH-X6tc0" class="photoswipe" data-pswp-width="482" data-pswp-height="226" data-once="photoswipePrepareGalleries"><img src="/sites/default/files/styles/landscape/public/2023-09/gen9B6C.tmp.jpeg.webp?itok=WVDq2IjX" width="1000" height="560" alt="Exputo quidem suscipere. Acsi " title="Camur cogo hendrerit paratus" loading="lazy" class="image-style-landscape"></a>
                </div>
                <div class="field__item">
                        <a href="http://local.blabla/sites/default/files/styles/original/public/2023-09/gen9A3C.tmp.png.webp?itok=5U5qcFjR" class="photoswipe" data-pswp-width="364" data-pswp-height="553" data-once="photoswipePrepareGalleries"><img src="/sites/default/files/styles/landscape/public/2023-09/gen9A3C.tmp.png.webp?itok=VSyZkiPH" width="1000" height="560" alt="Hendrerit quidne ut." title="Aliquam huic si sit zelus." loading="lazy" class="image-style-landscape"></a>
                </div>
        </div>
</div>

The correct approach would be IMHO the one where the field name is root. like the other fields right beside them.

Issue fork photoswipe-3384849

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

RgnYLDZ created an issue. See original summary.

rgnyldz’s picture

Title: Different/wrong html when there is only oneimage » Different/wrong html when there is only one image
grevil’s picture

Version: 5.0.0-alpha5 » 5.x-dev

Thanks for the issue report! I agree. 👍

anybody’s picture

Assigned: Unassigned » grevil

@Grevil: Let's please fix this and prepare a MR. I think it's important to solve this as early as possible, before many people are using / styling the structures.

Is this something we need to solve in this module or is this from the pswp library? Eventually let @thomas.frobieter also take a look for the right solution.

grevil’s picture

This is probably linked to the JS setting the fallback wrapper, have a more concise look at the wrapper:

<span class="photoswipe-gallery photoswipe-gallery--fallback-wrapper" data-once="photoswipe">

vs.

<div class="photoswipe-gallery" data-once="photoswipe">

I'll get to it, as soon, as I can!

grevil’s picture

This isn't easily done. Inside "/src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php", we have the following code, which causes this issue:

    if (!empty($items) && count($items) > 1) {
      // If there are more than 1 elements, add the gallery wrapper.
      // Otherwise this is done in javascript for more flexibility.
      $elements['#prefix'] = '<div class="photoswipe-gallery">';
      $elements['#suffix'] = '</div>';
    }

Prefixing "elements" unfortunately means prefixing the entire field, for some reason...

Meaning we probably need to move the entire logic inside our js, which ACTUALLY makes way more sense, since if we are using multiple images on a template using our twig method or the global photoswipe option, and no wrapper is manually set, each image will be wrapped independently!

EDIT: Upon further investigation, unfortunately, wrapping it on the php site only makes sense, since this issue is specific to the field formatter, any other a.photoswipe link needs to get wrapped individually (as a fallback of course), since we can't tell if multiple a.photoswipe links belong to one gallery or not, outside the field context.

anybody’s picture

Assigned: grevil » thomas.frobieter
Category: Bug report » Feature request

Yeah this was a very very large topic in the past... we shouldn't make too quick changes here. Now I also see the key difference: It's the fallback gallery wrapper vs. the intended gallery wrapper.

As this is expected behavior for reasons, I don't rate this as bug anymore, instead we should discuss how to unify the functionality, by for example only setting classes, but not adding wrappers. We need to discuss this personally in the team.

Switching to feature request for further discussions.

thomas.frobieter’s picture

Assigned: thomas.frobieter » grevil
Category: Feature request » Bug report

Why don't we just add the class to the field attributes then?
If the field wrapper is removed by the theme for whatever reason, the JS fallback kicks in.

thomas.frobieter’s picture

Assigned: grevil » thomas.frobieter
Category: Bug report » Feature request
anybody’s picture

It's not that simple. Many edge cases need to be considered, we shouldn't glitch back into the tales we were in before the current solution. No quick shots please, but of course suggestions like this are welcome for discussion.

grevil’s picture

I tinkered around a bit in the formatter and the reason why the whole field is prefixed, is because inside FormatterBase's "view()" method, the items from "viewElements" are simply merged with the "field" theme defintion:

$elements = $this->viewElements($items, $langcode);

    // If there are actual renderable children, use #theme => field, otherwise,
    // let access cacheability metadata pass through for correct bubbling.
    if (Element::children($elements)) {
      $entity = $items->getEntity();
      $entity_type = $entity->getEntityTypeId();
      $field_name = $this->fieldDefinition->getName();
      $info = [
        '#theme' => 'field',
        '#title' => $this->fieldDefinition->getLabel(),
        '#label_display' => $this->label,
        '#view_mode' => $this->viewMode,
        '#language' => $items->getLangcode(),
        '#field_name' => $field_name,
        '#field_type' => $this->fieldDefinition->getType(),
        '#field_translatable' => $this->fieldDefinition->isTranslatable(),
        '#entity_type' => $entity_type,
        '#bundle' => $entity->bundle(),
        '#object' => $entity,
        '#items' => $items,
        '#formatter' => $this->getPluginId(),
        '#is_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(),
        '#third_party_settings' => $this->getThirdPartySettings(),
      ];

      $elements = array_merge($info, $elements);

Leading to an incorrect hierarchy codewise:
screenshot

(The array entries 0 and 1 contain our "photoswipe_image_formatter" theme items and are therfore on the same level with the "field" theme)

I guess, this could be seen as a core bug, but on average this isn't a problem in 99% of all cases.

grevil’s picture

So my solution would be to create a separate 'photoswipe-image-formatter-gallery-wrapper' template, in which we define the gallery wrapper span / div and pass our "photoswipe-image-formatter" items into and display them.

This way we have a formatter specific solution which is really nice and clean and people can also hook into the gallery wrapper template and inject their own HTML if needed!

grevil’s picture

Status: Active » Needs work

I created a first MR, to show my approach. It's not perfect yet, currently I can only add the wrapper under the field-item or around the field. Not in between, so this still needs work (and discussion).

grevil’s picture

Status: Needs work » Needs review

All done, please review!

grevil’s picture

Assigned: thomas.frobieter » grevil

I'll adjust the tests tomorrow.

anybody’s picture

Thanks @Grevil, Code changes LGTM. For each broken test please manually check the new result is *really* correct. There's still a risk left that we forgot something or special things happen. We should use this chance :)

@thomas.frobieter should afterwards test the dev version, to be sure nothing is broken, before we tag a new release.

anybody’s picture

anybody’s picture

Priority: Normal » Major

@Grevil and @thomas.frobieter I think we should try to get this fixed ASAP as the code changes will be kind of BC. This means we need at least a 5.1.0 version then. Better to fix it earlier than leter.

grevil’s picture

Might take a bit, since I can not run photoswipe js tests locally.

grevil’s picture

All adjusted accordingly! Tests with two media/image fields vs. two one field with multiple pictures COULD be refined, but they are fine as is (there is a line checking for two gallery wrappers [2 fields] and for one gallery wrapper [1 field 2 pictures]).

This can be reviewed!

grevil’s picture

Assigned: grevil » anybody
anybody’s picture

Assigned: anybody » Unassigned
Status: Needs review » Reviewed & tested by the community

Changes LGTM. BC notes above. Think there's no way around it.

anybody’s picture

Status: Reviewed & tested by the community » Fixed

  • Anybody committed 74a11669 on 5.x authored by Grevil
    Issue #3384849 by Grevil, Anybody, thomas.frobieter: Different/wrong...
rgnyldz’s picture

I can confirm that the html structure is better and consistant now with the new version. Appreciate the work guys, thanks a lot ;)

Status: Fixed » Closed (fixed)

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