0

We are attempting to figure out how to properly add Chosen library to our custom theme. The main reason for doing this is to be able to add the Chosen JS files, but use a custom css file where we can control the output.

After reviewing Chosen Module we found that it adds a lot of bloat that we don't necessarily need and have found it difficult to override the css that is used.

Here is what we tried, but cannot get it to use the chosen.min.css and need help to see how it can be done.

mytheme.libraries.yml file

chosenjs:
    version: 1.x
    css:
       theme:
          css/chosen.min.css: { minified:true }
    js:
      js/chosen.min.js: { minified:true }

The chosen.min.js has the necessary settings to get the functionality we needed.

6
  • 1
    Is the indentation correct? It looks off.
    – Kevin
    Commented Jan 18, 2018 at 2:00
  • 1
    @Kevin yes it's off, indention should only be 2 spaces not 4.
    – No Sssweat
    Commented Jan 18, 2018 at 2:27
  • Under css, it should also be 2 spaces indented not 3. Commented Jan 18, 2018 at 11:57
  • @Kevin the code in my files have the correct indentation. When it was copied into editor it had extra spaces by accident. Not sure how that affects my questions as it remains unanswered, but it has been updated to two space. Commented Jan 18, 2018 at 17:25
  • YAML indentation is important and is a common error. Do these files exist in your theme at the correct paths? Also, how are you attaching it into Drupal?
    – Kevin
    Commented Jan 18, 2018 at 17:55

2 Answers 2

2

If the code posted above is correct, the indentation is off. It should look something like this:

search:
  version: VERSION
  js:
    extras/js/drupal/search/sort.js: {}
    extras/js/drupal/search/filter.js: {}

See how js is not indented under version?

The second part of it is you have to tell Drupal to load it either sitewide, or on demand:

Sitewide, you need to declare this in your theme info yaml:

libraries:
  - mytheme/search

If you want to do it on demand, something like:

/**
 * Implements hook_page_attachments_alter().
 */
function mytheme_page_attachments_alter(array &$attachments) {
  $current_path = \Drupal::service('path.current')->getPath();

  // We really want to minimize the loading of these two libraries
  // for anonymous users. so far, we only need it on any /search* path.
  if (preg_match('/\/search+/', $current_path)) {
    $attachments['#attached']['library'][] = 'core/drupal';
    $attachments['#attached']['library'][] = 'core/jquery';
    $attachments['#attached']['library'][] = 'mytheme/search';
  }
}

Or you can put it in a specific twig template i.e. node.html.twig like this

{{ attach_library('mytheme/search') }}
3
  • i updated that code in the original post. I added them as a library as you have shown above and it is working in my test scenarios. I have additionally updated my other comments. The only additional i made was I needed to add a Drupal behavior to handle the Chosen Options found on harvesthq.github.io/chosen/options.html Commented Jan 18, 2018 at 18:44
  • Yeah, you have to initialize it yourself. I forgot to mention that but assumed you had that. This is the stuff Chosen module takes care of for you.
    – Kevin
    Commented Jan 18, 2018 at 18:52
  • understood, but it added a lot of what i didn't need so the proposed solutions work for lighter implementation. Commented Jan 18, 2018 at 19:03
0

Do you want to use the un-compressed versions? Because after enabling Drupal aggregation for JavaScript, this will then compress and minify everything. In your example, you are using the minified versions. I also need to add Chosen and Select2. Chosen 1.8.7 is located on https://github.com/harvesthq/chosen/releases/tag/v1.8.7.

I need the 5 files listed below.

chosen.jquery.js
chosen.proto.js
chosen.css
chosen-sprite.png
[email protected]

My libraries file is the following.

global-styling:
  css:
    theme:
      css/style.css: {}

  js:
    js/chosen.jquery.js: {}
    js/chosen.proto.js: {}
    js/datatables-init.js: {}
    js/datatables.js: {}

Then inside the sass directory, I place chosen.css and use import so it gets compiled into the main CSS file. We already did this with the datatables, but it came with SCSS instead of CSS.

Where do the 2 images go? I place the 2 png image files inside /assets/images/. This is where all of the theme images go. chosen-sprite.png will not work in this folder because chosen.css is pointing to this image in the root directory the way it comes in the original library.

Also, it will be difficult to upgrade this library because the files are in 3 different places.

4
  • To make upgrading Chosen easy. Would it be better to create folders inside the subtheme called /themename/library/chosen. Then in libraries.yml point to the correct js location, then open the main sass file style.scss and point to the correct css location for compiling. THis way the whole Chosen library is together in one place. Commented Nov 29, 2018 at 15:33
  • i see a lot of questions in here. Are you expecting answers to them or are you just asking them to make your point? It might be best to create a separate question and reference this one if you are expecting an answer here. Commented Nov 29, 2018 at 20:33
  • I wanted to first ask if you knew you are using minified library version. And is that correct to do if you plan to enable js aggregation. Then I wanted to see if you learned anything new during the process of adding a third party library. And wanted to share information. Commented Nov 30, 2018 at 12:25
  • yes i was aware that we were using minified library version. I have altered the original question to include the minified: true to remove any ambiguity that may exist. Using the answer provided by Kevin I was able to learn the proper way to add the library and marked it as such. I am not really concerned about the images since I have locally hosted image I could use if needed. Commented Nov 30, 2018 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.