Try switching from select2 to tagify in JSON form
Select2 isn't compatible with the last version of Drupal. We've been using select2 for a long time but the underlying library is mostly abandoned. There has been no release for years and we need to pull in dev branch just to get it compatible with the current Jquery. Let's try swapping it for https://www.drupal.org/project/tagify and see if anything breaks?
It appears that neither widget Select2 or Tagify has the functionality and options that we need. We are going to complete more discovery
- Look at auto complete, and what it would take to extend in JSON form
Initial testing shows UI/UX improvements to using the tagify module. There are some limitations to tagify that will require more in depth testing and customization.
The module provides 2 different elements:
-
select_tagify: This element works well with our current approach to building the options in the dropdown. See WidgetRouter.php. No customizations were needed for the list to appear for publisher name, keywords and tags. The limiation are currently that it is not possible to create new keywords/tags directly in the field like you could with select2. The
SelectTagifyclass does not have any "#autocreate' capabilities. This would be something we need to look into more and possibly override to get the behavior we are looking for. The other issue I encountered was with the Publishers Name field letting me select multiple options which in theory shouldn't happen based on how I set up the config settings for the element. -
entity_autocomplete_tagify This element does not allow the user to provide the options for the dropdown which is an issue for us. It currently only lets you identify the target entity type 'example:node' down to the entity "data' if needed, one the user starts typing something it will autocomplete and show results in the field. I also ran into an issue where it would not allow me to create new tags within the element like you could with select2, this might just be because of a missing setting although it was not apparent to me what was missing.
The PR #4467 is currently using the select_tagify element. We can use this as a starting point to look into extending the code to allow us to autocreate new tags within the field.
I've looked into this a little more. I think we can still use the foundations from the tagify module if we want to do this but it's not super straightforward.
The entity_autocomplete_tagify element seems to offer a path, but it's a bit winding. Drupal's built-in entity_autocomplete can make use of a bunch of different plugin classes to customize its behavior. See for example, Link Selection Handler which gives some idea how we might move forward with this. Also it should in theory allow creation of new items via the #autocreate property but not sure how that can be customized.
This guide looks much closer to what we want to do, but uses #autocomplete_route_name to delegate more of the logic to an additional ajax route.
https://antistatique.net/en/blog/how-to-create-a-custom-autocomplete-using-the-drupal-8-form-api
Not sure how well entity_autocomplete_tagify stuff will integrate with that. Related:
https://antistatique.net/en/blog/how-to-create-a-custom-autocomplete-entity-field-widget-using-the-drupal-8-form-api
@zedmonds96, can you write up a summary and move this back to the backlog?
We may decide to change the way that handle titles going forward
I was mostly focused on the select_tagify element issue. I tried many different configurations of the select_tagify element in the Widget Router. I tried setting #allow_create to true, #enforce_whitelist to false, and other settings. Regardless of the configuration, new tags would not save. The form will appear to create the new tag, but is does not save. I cannot find the root of this issue.
A similar issue was discussed here: https://www.drupal.org/project/tagify/issues/3470650
However, they were having compatibility issues with another module so I am not sure if that's relevant.