How to allow to use JQuery selectors for the asNavFor attirbute

Created on 14 December 2023, 11 months ago
Updated 25 August 2024, 3 months ago

Problem/Motivation

Slick Slider allows to use not only classes or ids for the asNavFor Attribute like asNavFor =".slide-tabs" but also to use Query Selectors like asNavFor ="'.' + 'slide-tabs'"".

See an example here for using JQuery selector for asNavFor , that somebody coded: https://codepen.io/anchoor/pen/gKxNzw

But this does not work with the drupal implementation of slick.

I assume the reason is the way the data-slick attribute is encoded in slick.theme.inc:

$content_attrs->setAttribute('data-slick', Json::encode($json));

Is there a way to use JQuery selector in the asNavFor attribute?

Proposed resolution

Allow JQuery selector in the asNavFor attribute either by a code change or a proposal for a theme implementation using template_preprocess_slick.

Making this possible would greatly improve the possibilities to use asNavFor without the necessity of custom code.

Feature request
Status

Fixed

Version

3.0

Component

Documentation

Created by

🇩🇪Germany demonde

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

Comments & Activities

  • Issue created by @demonde
  • Status changed to Postponed: needs info 11 months ago
  • 🇮🇩Indonesia gausarts

    Thank you.

    Have you installed Slick Example?

    IIRC, 5 out of 6, more or less, are using asNavFor.

    No custom codes are needed.

    They would sync automatically as long as you provide the required optionset.

    That asNavFor option can and should be left empty as noted there for being overridden by formatters.

    I might misunderstand, so please detail your use case?

  • 🇩🇪Germany demonde

    My use case is as follows:

    I am having a paragraph "Slide Tabs" with a sub paragraph "Tab". The paragraph field "Tab" in "Slide Tabs" in redered by views via "Views field formatter." In the View I have a slick slide display of the view "block" with a slick attachement view "tabs" that control each other by asNavFor

    As a paragraph this can be rendered multiple times, so if the asNavFor target would be .slide-block it would control all slide tabs paragraphs which it should not.

    One solution would be to overwrite the asNavFor attribute by hook_slick_optionset_alter with $slick->setSetting('asNavFor', $target), but the available variables are missing the view dom_id which would enable to call .view-dom-[dom_id] .slide-block

    The better way is to achieve this by a JQuery asNavFor target that calls the parent .view class and subsequent the class of the other slick slide.

  • 🇩🇪Germany demonde

    In general I would like to be able to use such more complex approaches of using slick. AsNavFor is central to achieve these.

  • 🇩🇪Germany demonde

    Another solution would be to provide the views dom_id via hook_blazy_settings_alter or hook_slick_settings_alter or an object extension of blazy as a variable in $settings of my implenation of hook_slick_optionset_alter in my custom_slick_tabs_slick_optionset_alter.

    Something like:

    function custom_slick_tab_slick_optionset_alter(\Drupal\slick\Entity\Slick &$slick, array &$settings) {
      $target = ' . ' . $settings['view_dom_id'] . '.myslidertarget';
      $slick->setSetting('asNavFor', $target)
    }
    

    But I could not figure out how to do that.

  • 🇮🇩Indonesia gausarts

    > but the available variables are missing the view dom_id which would enable to call .view-dom-[dom_id] .slide-block
    Not sure if relevant to your needs, an existing sample is like here:
    https://git.drupalcode.org/project/blazy/-/blob/8.x-2.22/src/BlazyAlter....

    You can pass/write the found views_dom_id here to blazies object, and access it directly here for immediate change, or anywhere else.

    You can also get some pre-defined useful data at blazies object, such as blazies.css.id or blazies.view blazies.formatter array, etc.

    Most of blazies data are alterable via the provided hook_alters seen at blazy.api.php or slick.api.php.

    If not, perhaps regular hook_preprocess_slick, etc. are more direct, including changing asnavfor targets which are normally hard-coded to the .slick IDs to be simple. Similar blazies and slicks objects are also available at hook_proprocess.

  • 🇩🇪Germany demonde

    Thanks, but I am still not sure how to pass the dom_id to hook_slick_optionset_alter, blazySettingsAlter does not effect anything here. Being able to use jQuery targets forasNavFor would be the better solution.

  • Status changed to Fixed 3 months ago
  • 🇮🇩Indonesia gausarts

    Basically asNavFor is defined and associated by IDs for being unique and easily/ automatically defined:
    https://git.drupalcode.org/project/slick/-/blob/3.0.2/templates/slick.th...

    Such a design works perfectly for many carousels on a page. Normally inbuilt asNavFor is where both Slick main and Slick Thumbnail navigation is wrapped within slick-wrapper DIV.

    Association is the only key since you must define asNavfor at both main and thumbnail displays.
    You may want to see and learn how the HTML source of data-slick is constructed for both sliders to reconstruct your own.

    To break such a design, you just need to create separate sliders, and inject asNavFor as outlined above. Overriding existing designs might be done, but require more efforts.

    ID is not a must, might be classes, just be sure it exists on a page only once.

    I hope you can make it one or another :)

    Feel free to get in touch for paid custom works.

    Thanks.

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

Production build 0.71.5 2024