Different/wrong html when there is only one image

Created on 1 September 2023, over 1 year ago
Updated 28 September 2023, about 1 year ago

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.

Feature request
Status

Fixed

Version

5.0

Component

Code

Created by

🇹🇷Turkey rgnyldz

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

Comments & Activities

  • Issue created by @rgnyldz
  • 🇩🇪Germany Grevil

    Thanks for the issue report! I agree. 👍

  • Assigned to Grevil
  • 🇩🇪Germany Anybody Porta Westfalica

    @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.

  • 🇩🇪Germany Grevil

    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!

  • 🇩🇪Germany Grevil

    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!

    So very nice find, which is quite important actually! :)

  • Assigned to thomas.frobieter
  • 🇩🇪Germany Anybody Porta Westfalica

    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.

  • Assigned to Grevil
  • 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.

  • Assigned to thomas.frobieter
  • 🇩🇪Germany Anybody Porta Westfalica

    It's not that simply. 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.

  • 🇩🇪Germany Grevil

    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:

    (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.

  • 🇩🇪Germany Grevil

    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!

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    9 pass, 6 fail
  • @grevil opened merge request.
  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany Grevil

    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).

  • Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    Not currently mergeable.
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update over 1 year ago
    11 pass, 4 fail
  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany Grevil

    All done, please review!

  • Assigned to Grevil
  • 🇩🇪Germany Grevil

    I'll adjust the tests tomorrow.

  • 🇩🇪Germany Anybody Porta Westfalica

    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.

  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica

    @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.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update about 1 year ago
    11 pass, 4 fail
  • 🇩🇪Germany Grevil

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

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update about 1 year ago
    11 pass, 4 fail
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update about 1 year ago
    21 pass
  • 🇩🇪Germany Grevil

    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!

  • Assigned to Anybody
  • Issue was unassigned.
  • Status changed to RTBC about 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

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

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.0.7 + Environment: PHP 8.2 & MySQL 8
    last update about 1 year ago
    21 pass
  • Status changed to Fixed about 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇹🇷Turkey rgnyldz

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

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

Production build 0.71.5 2024