Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Could I get some guidance on applying this to an entity reference field? I've built the form alter, though I'm unclear on how to apply the ER to the options list:
'#options' => array('' => 'Pick something or enter your own', 1 => 'ABC', 2 => 'DEF', 3 => 'GHI'),
Comment | File | Size | Author |
---|---|---|---|
#39 | interdiff_dev.txt | 992 bytes | BlacKICEUA |
#39 | interdiff_beta.txt | 635 bytes | BlacKICEUA |
#39 | entity-reference-beta5-2678916-39.patch | 39.33 KB | BlacKICEUA |
#39 | entity-reference-support-2678916-39.patch | 39.27 KB | BlacKICEUA |
#37 | interdiff.txt | 1.51 KB | rodrigoaguilera |
Comments
Comment #2
kevinquillen CreditAttribution: kevinquillen at Velir commentedI am currently investigating this.
Form fields on content/entity forms are widgets, and not just vanilla Elements like you can use with custom forms in FormAPI.
I have a working version on my local, and will update dev shortly to provide widget support with settings.
Comment #3
kevinquillen CreditAttribution: kevinquillen at Velir commentedComment #4
kevinquillen CreditAttribution: kevinquillen at Velir commentedSo, while I sort of got it working on a List (text) widget, and it could work for entity reference, widgets are different in a few key ways.
One, in order to replicate the autocomplete functionality, I would need to reimplement the widget code - which looks like a lot of work to implement all of what selectize needs to replicate what Drupal is doing.
However... in the interim, I was able to get List (text) widgets working, which is in the latest code.
Comment #5
dnotes CreditAttribution: dnotes commentedFor 7.x there is a patch at #2835973: Support Field API for taxonomy, entityreference, select fields.
Comment #6
quironHi, I had the same issue, I need support for the
entity_reference
autocomplete widget.I developed a patch that works for me, basically:
field_types
entity_reference
, keeping all the configurable options'field_type'
in theelement['#settings']
and in the selectize element is uses to manage the different settingsHope it's useful for someone, also if works and passes the tests the module can have a new release including this new feature.
Comment #7
BlacKICEUAFixed undefined CARDINALITY in the SelectizeEntityReferenceWidget.
Fixed per field_type configurations in the Selectize element class.
Comment #8
BlacKICEUAComment #9
quironHi,
whats missing here to move it forward?
thanks.
Comment #10
kevinquillen CreditAttribution: kevinquillen at Velir commentedGreat patch here. I am trying to test it out with a term reference field. I set the type to "Autocomplete Tags style (selectize.js)", but I am getting this error in the console:
selectize.drupal.js?v=8.4.4:60 Uncaught TypeError: $.ajax(...).error is not a function
Comment #11
kevinquillen CreditAttribution: kevinquillen at Velir commentedOk, its because jQuery has deprecated those methods. Patch needs to be updated to be:
Comment #12
sunIndeed, we're still testing this patch, too. We'll follow up shortly with an improved version of the patch.
Also adding support for basic entit reference select drop-downs with a fixed / predefined list of options (the most basic use-case of e.g. categories).
Comment #13
kevinquillen CreditAttribution: kevinquillen at Velir commentedI am also having some UI issues with typing/autocomplete, and I can't seem to get the drop down select to show up if you click into the field (showing some limited list of options).
Also seeing the remove button show up on page load, pic attached.
The returned list of option matches also seem to be unsorted (this can be seen with one or two letters). I can't recall if the returned options were supposed to be alpha sorted or not if the match was in the middle of a word.
Comment #14
kevinquillen CreditAttribution: kevinquillen at Velir commentedThe above screenshot also prevents a placeholder from being attached to the element.
edit: Also just noticed that only 10 options are returned because the autocomplete is using TermSelection. Looks like the only way to override this is implementing our own per https://drupal.stackexchange.com/a/220521/57 and upping the limit to 100, or allow the user to configure it.
Comment #15
kevinquillen CreditAttribution: kevinquillen at Velir commentedForgive my JS here, just trying a few things.
When the element first loads, it adds an empty item div - I am not entirely sure why. I was able to undo this by adding an
onInitialize
event:Outside of that, it looks like there are two CSS issues at play. First, if you use the Field Group module and the element is within a vertical tab, you need to override this in your own style:
.vertical-tab { overflow: visible; }
Otherwise, the drop down list is hidden and cut off by the border of the tab area.
Second, the Selectize input has some style issue where clicking around inside of the input doesn't seem to have much of an effect except near the end of the element input area. For example, clicking near a tag doesn't trigger the drop down list, but clicking way over on the far right end of the element will trigger it (even then it is 50/50). I overcame this with:
.selectize-input { overflow: visible; }
With that, clicking anywhere in the input triggers the drop down list. This did not seem to break the widget in any way.
Will work on updating patches.
Comment #16
kevinquillen CreditAttribution: kevinquillen at Velir commentedJust realized I forgot to post a picture of the vertical tab CSS bug.
Comment #17
blasvicco CreditAttribution: blasvicco commentedHello, here another patch with more fixes.
https://www.drupal.org/files/issues/entity-reference-2678916.patch
Comment #18
kevinquillen CreditAttribution: kevinquillen at Velir commentedSome more bugs. It looks like you can save the field with referenced values, but when you come back to the entity form, the default values are not loaded into the field.
Also, saving values fails if the reference entity string contains a comma in its label, due to this:
This turns a term like "Foo, Bar, Baz (2)" into an array of:
Upon saving, this will cause Drupal to print "an illegal option has been detected" meaning this value is useless to it.
Comment #19
kevinquillen CreditAttribution: kevinquillen at Velir commentedThis code appears twice in Selectize.php:
Comment #20
kevinquillen CreditAttribution: kevinquillen at Velir commentedThere is a minor typo in the code comment, 'file' should be 'field'
Comment #21
kevinquillen CreditAttribution: kevinquillen at Velir commentedNot sure if its just an end of the day thing or not, but I am running in circles in the code.
The new field widget type
SelectizeEntityReferenceWidget
extendsEntityReferenceAutocompleteTagsWidget
however the form element inside of theformElement
method is having its type set toselectize
, which is triggering a pre/post rendering callback as well asvalueCallback
in theSelectize
class. This almost seems like a conflict, because the core entity reference form element and field widget classes work fine and set default values without an issue, however ours does not because the current form element is intended for just select lists.We need to have a better separation of concerns and intent so its easier to debug, instead of forks all around the code for 'entity_reference' vs 'list_string' as the original Selectize FormElement class only accounted for simple drop down lists, not autocomplete fields. I checked both the Chosen and Select2 Widget modules to see how they handle it, and neither one seems to tackle applying themselves to
EntityReferenceAutocomplete
widgets, onlyOptionSelectList
andSelect
.The way values and default values are parsed and set for autocomplete widgets vs basic select/option widgets are different and I don't think it can co-exist in the same class.
Comment #22
kevinquillen CreditAttribution: kevinquillen at Velir commentedAlso it will throw a fatal error if you reference an entity with comma or quotes in the name. I think because our Selectize form elements valueCallback is too simplistic for this type of field.
See: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21...
Comment #23
kevinquillen CreditAttribution: kevinquillen at Velir commentedLast patch will need a re-roll, it won't apply cleanly anymore after the last two commits.
Comment #24
kevinquillen CreditAttribution: kevinquillen at Velir commentedI am testing with adding "entity_reference" as a supported type to the original Selectize form element. It looks like it works well - except for autocomplete based widgets, for the reasons outlined above.
I don't have those same issues with an entity reference with using "Selectize" as the form widget. The dev branch now has the change that allows the original widget to work with Entity Reference.
I think if you want to move forward with Selectize applying to an entity reference field but using Autocomplete, it should be split out to a new widget and a new form element (perhaps extending EntityAutocomplete?) because theres a little more work to do there to make that work. There is also the core issue of autocomplete fields not liking comma or quotes in a term name, which is what I ran into with the above patch but wasn't sure how to fix it unless the values are stored as objects instead of a string.
Of course the obvious caveat is that someone should not enable "Selectize" for a field that could have tons of results... which is where you'd want the Selectized autocomplete... and I think this patch is almost there, but needs work.
Comment #25
kevinquillen CreditAttribution: kevinquillen at Velir commentedComment #26
BlacKICEUASo, I spent some time for Entity Reference functionality for the Selectize module. All my work based on previous solutions from this issue.
I think, I found the working solution and also added some extra features for widgets.
Tested:
- entity_reference tag widget;
- entity_reference simple select widget;
- handled possibility for creation entities via widget (like: Create referenced entities if they don't already exist)
Added sort setting for a widget (field/direction).
Fixed summary for a widget.
Added new form element ('selectize_entity_autocomplete') based on 'selectize' form element.
Changed composer.json (added library requirment and repository).
Comment #27
BlacKICEUAAnother implementation for the EntityReferenceAutocomplete:
- Form Element extends EntityAutocomplete;
- Form Widget extends EntityReferenceAutocompleteTagsWidget;
- Added additional options for the AutoCreate (Create On Blur, Create filter(REGEX))
Comment #28
kevinquillen CreditAttribution: kevinquillen at Velir commentedI also noticed that autocomplete matcher methods exist and can be overrridden, I also noticed that Selectize has an option to change the default delimiter from a comma to whatever, like triple pipes. Perhaps that would solve the issue of referenced items having commas or quotes in their label.
I will give this patch a look.
Comment #29
kevinquillen CreditAttribution: kevinquillen at Velir commentedThis patch is looking pretty good. Although there is one thing I can't narrow down.
In this screenshot, the values and labels are messed up. I think it is because the default value has a quote added (because this term has commas in the name and is escaped when labels are created).
This causes selectize to parse it out into 3 different labels when it should be one item.
Comment #30
BlacKICEUAExtended Widgets functionality - added possibility to specify delimiter. Used ";" - as default delimiter symbol for Autocomplete.
Some changes for the "Select" element:
- FormElement Selectize extends Select;
- FieldWidget SelectizeWidget extends OptionsSelectWidget;
Comment #31
fabian.marz CreditAttribution: fabian.marz at netzstrategen commentedEnhanced patch 30 with following:
- parse the config only if it's not already an object, thus the config can be extended with callback functions from other modules
- added a config to hide the term id from the values to prevent value duplicates when creating them on the fly or using autocomplete
Comment #32
fabian.marz CreditAttribution: fabian.marz at netzstrategen commented@BlacKICEUA, is there a particular case why did you use
over this?
The upper code block threw some errors when trying to save a reference field without IDs in the values with just one entity referenced in the field config. The new patch fixes the functionality
Comment #33
sunLots of unnecessary changes to files in this patch...
The overridden methods of this class never calls into the parent methods.
We should try to avoid duplicating code of the parent class and reuse it instead. Are we doing that?
$access should really be pre-determined by #access on the form element at this point of processing (possibly by the parent class code), since we don't want to be involved with that kind of logic (at all).
…cancelled right there with my review, as I wasn't sure how much is being duplicated here without solid reason. Even if that problem exists in core, we can and should still work around it using traits or helper classes here.
Comment #34
BlacKICEUA@fabian.marz
This part from core. No changes added by me.
@sun
1. Added new lines
https://www.drupal.org/node/318#indenting
2. Overrided methods in the SelectizeEntityAutocomplete:
- function valueCallback - overrided call to static::getEntityLabels($element['#default_value'], $delimiter); (delimiter - added)
- function processAutocomplete (from FormElement) - overrided element #attributes and added processing for selectize settings.
- function validateEntityAutocomplete - overrided $input_values extract functionality.
- function getEntityLabels - added $delimiter.
Non-overrided methods used from parent classes:
processEntityAutocomplete, matchEntityByTitle, extractEntityIdFromAutocompleteInput ...
3. This is checks a named route with parameters against applicable access check services.
In case it - false we simple return textfield.
This is only my patch proposal.
Comment #35
quironre-rolled #32 to last version beta5 removing a not related change.
Comment #36
quironAdding a new patch version to handle with entities with duplicated labels in the referenced taxonomy. The hide ID was wrongly implemented as long as was removing the ID both from the label and the value. Now it removes only from the label.
Comment #37
rodrigoaguileraPatch 36 is not applying for me so I tried to re-do it based on the simple interdiff. I also attach a proper interdiff between 35 and 37.
I also include a version of the patch for the beta5 "stable" release.
Comment #38
BlacKICEUAFixed small issues in the src/Element/Selectize.php. (Added additional use statements.)
Updated Selectize library up to v0.12.6
Included two patches for 1.x-dev and beta5 releases.
Attached interdiff between 37 and 38.
Comment #39
BlacKICEUASorry. Wrong files was uploaded early.
Comment #40
quironTested #39 and worked for me. Also, the solution IMHO is correct.
@kevinquillen how can I help to move this forward?
thanks!
Comment #41
Guido_SNothing new here and on this module in general since almost 3 years, although it's tested & reviewed by the community?
I would really love to use selectize as a widget in drupal 8.
Comment #42
PasqualleThe composer.json changes should go into readme or install.txt file as external dependencies do not work from module.
https://www.drupal.org/docs/creating-custom-modules/add-a-composerjson-f...
https://www.drupal.org/docs/develop/documenting-your-project/module-docu...
#3198417: Official support for npm
Comment #43
PasqualleComment #45
Pasquallecommitted without the composer.json changes
The library installation can continue in #2949060: Add composer drupal-library information to composer.json