Problem/Motivation
When accessing a form using Multiselect, console shows the error "Uncaught TypeError: Cannot read properties of undefined (reading 'widths')".
Steps to reproduce
- Create a form with a multiselect form element type.
- Visit the form page.
- Check the browser console. The error should be there.
Proposed resolution
The multiselect module loads the multiselect.js. In the line 68 there is this code:
if (drupalSettings.multiselect.widths) {
var widths = drupalSettings.multiselect.widths;
$(context).find("div.multiselect-available, div.multiselect-selected, select.form-multiselect").width(widths);
}
The drupalSettings.multiselect.widths value is not properly set and this generates the error.
As a workarround I suggest to check if drupalSettings.multiselect exist and provide a default value for widths if don't.
Comments
Comment #2
omkar-pd commentedWill work on this.
Comment #3
espurnesProvided patch to remove the error.
Comment #4
omkar-pd commented@espurnes I just found out that the library is not getting attached, So with your solution, the 'widths' will always be the same.
Comment #5
omkar-pd commentedhook_page_build is deprecated, hence library was not getting attached. used hook_page_attachments.
Now users can change the setting dynamically.
Comment #6
damienmckennaThis resolves the problem for me, marking #5 RTBC.
FYI I was seeing the following error in Safari:
Comment #7
espurnesHello omkar-pd,
it is a much better approach. Thanks for the patch, it solves the issue.
Comment #9
mandclu commentedThis issue (and particularly the resolution) is mostly a duplicate of #3009259: Missing javascript settings. There are a couple of coding style differences here that I think are worth including, so merging in those.