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

espurnes created an issue. See original summary.

omkar-pd’s picture

Assigned: Unassigned » omkar-pd

Will work on this.

espurnes’s picture

Provided patch to remove the error.

omkar-pd’s picture

@espurnes I just found out that the library is not getting attached, So with your solution, the 'widths' will always be the same.

omkar-pd’s picture

Assigned: omkar-pd » Unassigned
Status: Active » Needs review
StatusFileSize
new783 bytes
new39.69 KB

hook_page_build is deprecated, hence library was not getting attached. used hook_page_attachments.
Now users can change the setting dynamically.

damienmckenna’s picture

Status: Needs review » Reviewed & tested by the community

This resolves the problem for me, marking #5 RTBC.

FYI I was seeing the following error in Safari:

[Error] TypeError: undefined is not an object (evaluating 'drupalSettings.multiselect.widths')
(anonymous function) (drupal.js:16)

espurnes’s picture

Hello omkar-pd,

it is a much better approach. Thanks for the patch, it solves the issue.

  • mandclu committed 8a84015 on 2.x
    Issue #3273331 by espurnes, omkar-pd, mandcl: Cannot read properties of...
mandclu’s picture

Version: 8.x-1.2 » 2.x-dev
Status: Reviewed & tested by the community » Fixed
Related issues: +#3009259: Missing javascript settings

This 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.