Chosen library and settings are not attached if preRender is not called

Created on 26 July 2018, over 6 years ago
Updated 7 March 2023, about 2 years ago

Hi everyone,
I just installed chosen on drupal 8 and set it on configuration page to apply always and on every pages!
But its only appears on admin pages and not on frontend areas.
Just look into the rendered page, on chosen script just write down state:disabled!

So how can I fix it?

Thanks a lot :)

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany sonixax

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States froboy Chicago, IL

    I've confirmed this issue as stated in #2988400-12: Chosen library and settings are not attached if preRender is not called β†’ .

    In some situations, if Drupal's render pipeline does not know about an element that Chosen has a callback for (currently select, date combo, or select other), then the Chosen library and settings are not attached.

    Some possible scenarios:
    - An element is written directly into a twig file
    - An element is loaded by external JS (via an embed)

    Additionally if the Chosen JS library is loaded by a custom theme, this can result in cases where the JS is loaded but the settings are not passed to drupalSettings, so all select:visible elements have Chosen applied instead of the elements selected in the "Apply Chosen to the following elements" field.

    Possible resolutions:

    1. #2988400-12: Chosen library and settings are not attached if preRender is not called β†’ - call chosen_attach_library directly where you need it in your theme, block, etc. (This could be added to the docs somewhere.) A brute force approach might look like this:

    function my_theme_preprocess_html(&$variables) {
    ...
        chosen_attach_library($variables['page']);
    ...
    }
    

    2. Update the "Use chosen for admin pages and/or front end pages" field
    - Change the existing options to read like "Include Chosen on every page that contains elements it can act on".
    - Add a new option: "Include Chosen on every page (even if Drupal doesn't know about elements Chose can act on)"

Production build 0.71.5 2024