Option to load swiper on all pages

Created on 6 July 2023, over 1 year ago
Updated 5 April 2024, 8 months ago

Problem/Motivation

At some point (mostly lack of features like breakpoints, thumbs etc) we need to trigger swiper from within JS manually. But as I understand the swiper JS libraries are not loaded if there is no swiper present at that page with this module. So we need to add swiper libaries to html.html.twig (not a great solution but works). The issue is now we have two libraries at some pages. Also difference beween versions cause some problems in rare cases

Steps to reproduce

Install fresh drupal and swiper formatter module. Try to kick in a swiper with regular JS.

Proposed resolution

A great option would be to have a checkbox on the swiper teplates page. When enabled, the CSS and JS library should load on every page.

An option to define patterns would be amazing but not critical I think.

User interface changes

Settings page will have checkbox to load swiper libraries on all pages

Feature request
Status

Closed: works as designed

Version

1.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
  • 🇳🇱Netherlands nk_

    Never put assets directly in Twig/HTML, this is for what you have libraries in Drupal.

    Your best bet, very standard Drupal way for the situation when you want library literally everywhere, and that it does not multiply and conflict is this

    /**
     * Implements hook_page_attachments_alter().
     */
    function YOUR_MODULE_NAME_page_attachments_alter(&$page) {
       
       /* Attach this library just everywhere, with no config on pages/scopes where is normally triggered. */
       
       // If you use default, CDN current version
       $page['#attached']['library'][] = 'swiper_formatter/remote';
       
       // OR remove previous line and have this one uncommented, if you are using local version.
       // $page['#attached']['library'][] = 'swiper_formatter/local';
     
    }
    

    Or you can also try something like
    {{ attach_library('swiper_formatter/remote') }}
    in your twig (page.html.twig or html.html.twig) but I really do not recommend this.

  • 🇳🇱Netherlands nk_

    As for Breakpoints you mentioned, see here how you can easily implement it:
    https://www.drupal.org/project/swiper_formatter/issues/3318311#comment-1... 💬 Responsive breakpoints Needs work

  • Status changed to Closed: works as designed 8 months ago
  • 🇳🇱Netherlands nk_
Production build 0.71.5 2024