Problem/Motivation

User story:

Given that I have a large number of options in a Views Jump Menu
As a user of the jump menu
I want to be able to search/filter the list of options
So that I can find the option that I am looking for.

It would be nice to be able to use the Views Jump Menu with the Chosen or Select2 JavaScript libraries.

More information:

  1. Chosen:
    1. homepage & demos, source code
    2. Drupal integration module
    3. npm, asset-packagist
  2. Select2:
    1. homepage, demos, source code
    2. Drupal integration module
    3. npm, asset-packagist

Proposed resolution

  1. Add a service to detect the presence of extra libraries which could be used to enhance the jump menu
  2. Add a control to the views_jump_menu format settings to let an administrator select an extra library; add an update hook to modify existing views_jump_menu configurations to add an (empty) extra library configuration option to ensure the config model is up-to-date
  3. Modify the theme code to detect if an extra library has been selected, and if so, attach that extra library to the render output
  4. Update tests to detect that extra libraries are output if selected

Remaining tasks

  1. Write a patch
  2. Review and feedback
  3. RTBC and feedback
  4. Commit

User interface changes

Adds an "Include extra library" control to the Views Jump Menu format settings if an extra library can be detected.

API changes

Adds a hook_views_jump_menu_extra_libraries_alter(&$extraLibrariesList) to allow modules/themes which define libraries to indicate that those libraries could be used with views_jump_menu.

Data model changes

Adds an extra_library string to the jump menu views schema, to track which extra library should be used with that jump menu.

Release notes snippet

Added optional support for extra JavaScript libraries to enhance the behavior of the jump menu.

Original report by jbfelix

Hello,

How to apply chosen or select2 library ?

Thanks

Comments

jbfelix created an issue. See original summary.

dbielke1986’s picture

I need exactly the same...

hartsak’s picture

I was able to integrate a views jump menu with select2 by copying the modules/contrib/views_jump_menu/templates/views-jump-menu.html.twig file to my theme and modifying it a bit:

First attach the select2 library
{{ attach_library('select2/select2') }}

Then modify the select element

Original code:

  <select{{ select.attributes }}{% if select.select_label %} aria-label="{{ select.select_label }}"{% endif %}>

select-element with select2 support (you can add whatever options you need here):

  <select data-select2-config='{"multiple":false,"allowClear":false,"tags":false,"theme":"default","maximumSelectionLength":0,"tokenSeparators":[],"selectOnClose":false,"width":"100%","minimumResultsForSearch":10}'{{ select.attributes.addClass('select2-widget') }}{% if select.select_label %} aria-label="{{ select.select_label }}"{% endif %}>

I believe it would be possible to integrate a jump menu with the chosen library in a similar manner, but I have not tested it yet. Hope that helps you out with select2, though!

mparker17’s picture

Category: Support request » Feature request
Issue summary: View changes

Updated issue summary

mparker17’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new21.99 KB

Here is a patch which adds support for the Chosen and Select2 modules (other modules can add their own support by implementing hook_views_jump_menu_extra_libraries_alter()).

To use it:

  1. Make sure that the Chosen-Drupal integration module and/or the Select2-Drupal integration module have been downloaded and enabled.
  2. Make sure the corresponding JavaScript library has been downloaded to the location expected by the integration module (check the site's status report: both the Chosen and Select2 modules will show an error there if the JavaScript library is in the wrong location).
  3. Edit the view display which uses the Views Jump Menu, and, under Format, click "Settings" next to "Jump Menu". Scroll to the bottom of the page, and choose a library from the "Include extra library" section. If necessary for the JavaScript library that you are using, add a Wrapper class and/or Select class - see the JavaScript library's documentation for more information. Apply your new settings, and Save the view.
  4. Visit the view display to verify.
mparker17’s picture

StatusFileSize
new21.99 KB
new957 bytes

Found and fixed some coding standards issues.

mparker17’s picture

StatusFileSize
new22.21 KB
new789 bytes

I doubt this is the best solution, but here is a work-around for #3162684: Add a chosen/drupal.chosen.all library that also includes the CSS

colan’s picture

Status: Needs review » Needs work

This looks fairly comprehensive, which is great. Would you be able to add the info in #5 to the README? I didn't do much of a code review, but noticed that the docs there weren't added anywhere.

mparker17’s picture

Status: Needs work » Needs review
StatusFileSize
new23.68 KB
new1.42 KB

Thanks for the feedback! Here's a new patch with an updated README. Additional feedback welcome!

colan’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/views/style/JumpMenu.php
@@ -111,6 +113,52 @@ class JumpMenu extends StylePluginBase {
+    return \Drupal::getContainer()->get('views_jump_menu.extra_library');

Not sure if we need to be calling \Drupal here. Would it be possible to inject the service into the plugin as discussed at https://drupal.stackexchange.com/a/239868/2089, and then we can simply drop this method?

I'm realizing that the info there should probably be added to the Dependency injection in Plugin (Block) doc page. (I just marked it as incomplete, and referred to the Drupal Answer.)

+++ b/views_jump_menu.install
@@ -0,0 +1,63 @@
+  if (!isset($sandbox['viewsConfigNames'])
+  ) {

Should be combined into one line.

mparker17’s picture

@colan, thanks for the code review!

Not sure if we need to be calling \Drupal here. Would it be possible to inject the service into the plugin as discussed at https://drupal.stackexchange.com/a/239868/2089, and then we can simply drop this method?

Looks like I can inject the service!

However, I'd suggest leaving the method - from experience, I've found accessing dependencies through an accessor method to be more portable - the code can be agnostic about how dependencies are injected when overriding the class, copying code elsewhere, or mocking dependencies in tests.

colan’s picture

👍

mparker17’s picture

Status: Needs work » Needs review
StatusFileSize
new25.14 KB
new2.81 KB

Here's a new patch. Reviews welcome!

colan’s picture

Status: Needs review » Reviewed & tested by the community

Code looks good, and works well in my testing.

It actually didn't work right after setting it up, but then I came back to it later, and it did work. So a cache clear probably needed to happen.

mrpauldriver’s picture

A already have Select2 module installed and working for views exposed filters but I am struggling to get this working for views output.

I expect it is just a matter of not using the correct wrapper and select classes.

Could somebody put me on the right track please?

carolpettirossi’s picture

Just adding here that I tested patch provided on #13 and it works perfectly in my case (Chosen) with the DEV version.

However, the patch doesn't apply successfully to the beta3 version which forces me to use the DEV version. When should we expect a new release for this module?

mparker17’s picture

StatusFileSize
new19.23 KB
new25.14 KB

@carolpettirossi, I've re-rolled the patch from #13 without tests as 3077459-17-on-beta3.do-not-test.patch so it'll apply to beta3.

I'm not a maintainer so I can't create a new release. I believe the last person to create a release was @jcnventura


Since I made a mistake with the patch filename in #13 and I'm uploading a different patch file here for @carolpettirossi, I'm re-uploading the "real" patch from #13 as 3077459-17--views_jump_menu--support-chosen-select2.patch to (hopefully) avoid confusion about which patch is the most recent.

There are no changes between the patch in #13 and 3077459-17--views_jump_menu--support-chosen-select2.patch, so I'm not uploading an interdiff, and I'm leaving the issue RTBC.


@MrPaulDriver, I might be able to help; but first I need to know if you've applied the patch and then followed the steps in #5.

jcnventura’s picture

I think the fact that this is RTBCd helps a lot in the possible creation of a new release. I'd like to see an RTBC++, if possible before commtiting this.

replicaobscura’s picture

I have only tested this a small amount but have not come across any issues so far. I'll let at least one or two others hopefully confirm, then I think we are good to go anytime.

jcnventura’s picture

StatusFileSize
new0 bytes

Re-roll of #17 for the current version.

jcnventura’s picture

And now with bytes in it.

jcnventura’s picture

Aargh.. Previous patch included the patch from #17..

Status: Reviewed & tested by the community » Needs work
den tweed’s picture

Status: Needs work » Needs review
StatusFileSize
new10.98 KB

Re-roll for 1.0 release

den tweed’s picture

StatusFileSize
new23.52 KB

Previous patch didn't include the new files, fixed that