When using the directory browser with a field which has a limited number of values, a javascript error is thrown when trying to select an entity.

Uncaught TypeError: Cannot read property 'toString' of null
    at Object.Drupal.checkPlain (drupal.js?v=8.8.0:51)
    at drupal.js?v=8.8.0:61
    at Array.forEach (<anonymous>)
    at Object.Drupal.formatString (drupal.js?v=8.8.0:58)
    at Object.Drupal.t (drupal.js?v=8.8.0:115)
    at Object.Drupal.formatPlural (drupal.js?v=8.8.0:160)
    at Object.selectionChanged (media_directories_ui.toolbar.js?q2csr8:110)
    at Object.Drupal.MediaBrowser.clearMediaSelection (media_directories_ui.browser.js?q2csr8:68)
    at HTMLDivElement.<anonymous> (media_directories_ui.media.js?q2csr8:67)
    at HTMLDivElement.dispatch (jquery.min.js?v=3.4.1:2)
Drupal.checkPlain @ drupal.js?v=8.8.0:51
(anonymous) @ drupal.js?v=8.8.0:61
Drupal.formatString @ drupal.js?v=8.8.0:58
Drupal.t @ drupal.js?v=8.8.0:115
Drupal.formatPlural @ drupal.js?v=8.8.0:160
selectionChanged @ media_directories_ui.toolbar.js?q2csr8:110
Drupal.MediaBrowser.clearMediaSelection @ media_directories_ui.browser.js?q2csr8:68
(anonymous) @ media_directories_ui.media.js?q2csr8:67
dispatch @ jquery.min.js?v=3.4.1:2
v.handle @ jquery.min.js?v=3.4.1:2

It is caused by 'remaining' constant which has null value.

Comments

gresko8 created an issue. See original summary.

gresko8’s picture

Status: Active » Needs review
StatusFileSize
new1.13 KB

Attaching a patch that fixes it by calculating the remaining items from cardinality and selection length.

ytsurk’s picture

Assigned: Unassigned » ytsurk

Thanks for the catch and the patch. I'll review and merge it in tomorrow.

All this ES6 code will be downgraded to ES5 soon .. #3099106: Downgrade ES6

ytsurk’s picture

ytsurk’s picture

StatusFileSize
new1.78 KB

I could no longer reproduce your error ... I tested with a media field having cardinality 3.
Anyway, I change here @rang501 initialization quick-fix introduced in #3101800: Javascript error when opened from CKEditor, and now properly initialize remainingItems in the object with 1.

Also formatPlural is now used the right way. FYI @gresko8 @count is not needed and will take the first argument. Also the singular translation shall not use @count, see formatPlural docs.

ytsurk’s picture

Assigned: ytsurk » rang501
ytsurk’s picture

@rang501 - can you review this and merge it in if it fits for you, please?

  • ytsurk committed 8112f95 on 8.x-1.x
    Issue #3100530 by ytsurk, gresko8: Unable to select entities on fields...
ytsurk’s picture

Assigned: rang501 » Unassigned
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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

ytsurk’s picture

I incidentally removed the possibility to select multiple items on the overview page and fixed that here https://git.drupalcode.org/project/media_directories/commit/a3aed1a