Galerie image

Utilité Affichage mosaique et galerie plein écran au clic
Infos utiles Voir site https://inventairepierreseche.be/?BergesRuisseauDuHouripont4

Code généré avec l'aide de Claude
Exemple de code

Dans le template twig

{% set imageFields = ['imagebf_image', 'imagebf_image1', 'imagebf_image2', 'imagebf_image3', 'imagebf_image4'] %}
<div class="fiche-images">
    {% for field in imageFields %}
        {% set original = fiche[field] ?? '' %}
        {% if original is not empty %}
            {% set parts = original|split('_') %}
            {% set n = parts|length %}
            {% if n &gt; 2 %}
                {% set thumb = 'cache/' ~ parts|slice(0, n - 2)|join('_') ~ '_vignette_400_300_' ~ parts|slice(n - 2)|join('_') %}
                <a href="files/%7B%7B%20original%20%7D%7D" class="fiche-zoom{{ loop.first ? ' fiche-zoom-main' : '' }}">
                    <img class="{{ loop.first ? 'center custom-entry' : 'fiche-thumb' }}" alt="{{ fiche.bf_titre|e('html_attr') }}{{ loop.first ? '' : ' (' ~ loop.index ~ ')' }}" src="%7B%7B%20thumb%20%7D%7D" width="400" height="300" />
                </a>
            {% endif %}
        {% endif %}
    {% endfor %}
</div>


Dans Page CSS

.fiche-images { display: flex; flex-wrap: wrap; gap: .5rem; }
.fiche-images .fiche-zoom-main { flex: 0 0 100%; }
.fiche-images .fiche-thumb { width: 90px; height: auto; border-radius: 4px; }
.fiche-images a { cursor: zoom-in; }

#fiche-lightbox { border: 0; padding: 0; background: transparent; max-width: 95vw; max-height: 95vh; }
#fiche-lightbox::backdrop { background: rgba(0, 0, 0, .85); }
#fiche-lightbox img { display: block; max-width: 95vw; max-height: 95vh; object-fit: contain; cursor: zoom-out; }
#fiche-lightbox button { position: absolute; top: .5rem; right: .5rem; border: 0; border-radius: 50%; width: 2rem; height: 2rem; font-size: 1.25rem; line-height: 1; cursor: pointer; }

#fiche-lightbox .fl-prev,
#fiche-lightbox .fl-next {
  position: fixed; top: 50%; transform: translateY(-50%);
  width: 3rem; height: 3rem; border: 0; border-radius: 50%;
  font-size: 2rem; line-height: 1; cursor: pointer;
  background: rgba(255, 255, 255, .85);
}
#fiche-lightbox .fl-prev { left: 1rem; }
#fiche-lightbox .fl-next { right: 1rem; }
#fiche-lightbox .fl-counter {
  position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%);
  color: #fff; font-size: .9rem; text-shadow: 0 0 4px #000;
}
/* une seule image : pas de flèches */
#fiche-lightbox.single .fl-prev,
#fiche-lightbox.single .fl-next { display: none; }


Fichier lightbox.js à placer dans custom/javascripts

if (!window.__ficheLightbox) {
  window.__ficheLightbox = true;

  var links = [], index = 0, dlg = null, img = null, counter = null;

  function build() {
    dlg = document.createElement('dialog');
    dlg.id = 'fiche-lightbox';
    dlg.innerHTML =
      '' +
      '×' +
      '‹' +
      '›' +
      '<span class="fl-counter"></span>';
    document.body.appendChild(dlg);

    img = dlg.querySelector('img');
    counter = dlg.querySelector('.fl-counter');

    // clic sur l'image ou le fond : fermer ; clic sur un bouton : action
    dlg.addEventListener('click', function (e) {
      var btn = e.target.closest('button');
      if (btn) {
        e.stopPropagation();
        if (btn.classList.contains('fl-prev')) show(index - 1);
        else if (btn.classList.contains('fl-next')) show(index + 1);
        else dlg.close();
        return;
      }
      dlg.close();
    });

    // flèches du clavier (Échap est géré nativement par )
    dlg.addEventListener('keydown', function (e) {
      if (e.key === 'ArrowLeft') { e.preventDefault(); show(index - 1); }
      else if (e.key === 'ArrowRight') { e.preventDefault(); show(index + 1); }
    });

    // swipe sur mobile
    var startX = null;
    dlg.addEventListener('touchstart', function (e) { startX = e.touches[0].clientX; }, { passive: true });
    dlg.addEventListener('touchend', function (e) {
      if (startX === null) return;
      var dx = e.changedTouches[0].clientX - startX;
      startX = null;
      if (Math.abs(dx) &gt; 50) show(index + (dx &lt; 0 ? 1 : -1));
    });
  }

  function show(i) {
    index = (i + links.length) % links.length; // boucle : après la dernière, on revient à la première
    var link = links[index];
    var thumb = link.querySelector('img');
    img.src = link.href;
    img.alt = thumb ? thumb.alt : '';
    counter.textContent = links.length &gt; 1 ? (index + 1) + ' / ' + links.length : '';
    dlg.classList.toggle('single', links.length &lt; 2);

    // précharge la photo suivante
    if (links.length &gt; 1) {
      new Image().src = links[(index + 1) % links.length].href;
    }
  }

  document.addEventListener('click', function (e) {
    var link = e.target.closest('a.fiche-zoom');
    if (!link) return;
    e.preventDefault();

    if (!dlg) build();

    // toutes les images de la même fiche
    var container = link.closest('.fiche-images') || link.parentNode;
    links = Array.prototype.slice.call(container.querySelectorAll('a.fiche-zoom'));

    show(links.indexOf(link));
    dlg.showModal();
  });
}
Langage TWIG