Media entities returning ints for IDs breaks Media Library

Created on 9 December 2022, over 1 year ago
Updated 6 December 2023, 6 months ago

Problem/Motivation

When passing real integers (not string-ints) to \Drupal\media_library\Ajax\UpdateSelectionCommand, Media Library will duplicate each ID leading to Media Library exceeding its cardinality (overflow).

UpdateSelectionCommand is documented as receiving @param int[] $media_ids, however in practice string[] is passed. If int[] is strictly passed, then Media Library will break.

Steps to reproduce

  1. Either:
    1. Update Media entity to return a strict int from its id() function, such as via Bundle Classes, or coercing the core Media entity class.
    2. Or, modify \Drupal\media_library\Ajax\UpdateSelectionCommand, coerce each value to an integer, in the constructor: $this->mediaIds = array_map('intval', $media_ids);.
  2. Use Media Library to upload a new Media, such as via Media Instagram.

After completing the form and saving for the first time, you'll see +2 added to your selection counter. Which may exceed field cardinality and prevent you from closing the media library window:

When debugging, you'll find a mix of strings/ints in your JS state:

Proposed resolution

Coerce to ints in Command, and coerce to ints in media library JS.

Note: Fix in πŸ› It is possible to overflow the number of items allowed in Media Library Fixed has no effect.

Remaining tasks

Implement.

User interface changes

Fixes UI bug when PHP code is customised.

API changes

Fix data types to follow documented types.

Data model changes

Drupal.MediaLibrary.currentSelection contains ints not strings

Release notes snippet

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
MediaΒ  β†’

Last updated 1 day ago

Created by

πŸ‡¦πŸ‡ΊAustralia dpi Perth, Australia

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024