Improving JS behavior to comply with field cardinality

Created on 25 January 2018, over 6 years ago
Updated 12 September 2023, 10 months ago

Problem

When a Media Entity reference field instance has configured a limited number of references via the cardinality setting, The Media Entity Browser javascript does not validate that the number of selected items does not exceed the configured cardinality. As a result, if the user select more than the allowed cardinality items the references will not have the expected result.

This problem affects other entity browser modules such as "File Browser" but they claim there is no general solution that would work for all entity browser implementations.

Resolution

This solution ports the patch for "File Browser" module #2917823: Supporting cardinality (i.e. preventing how many items can be selected with JS) β†’ to "Media Entity Browser" version 2.x (the version that supports Media in core).

Example
A field with cardinality = 2:

The improved JS behavior will limit the selection of reference items to 2 and display a count of current selected items and warning message when you reach the cardinality limit:

Credits

Most of the code was ported from patch https://www.drupal.org/files/issues/support-cardinality-in-views-selecti... β†’ coded by @JurriaanRoelofs so most of the credit is for the great work he did for File Browser.

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain citlacom

Live updates comments and jobs are added and updated live.
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.

  • πŸ‡ͺπŸ‡¨Ecuador jwilson3

    patch in #17 no longer applies to 2.0-alpha4

  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States loze

    Although this patch does allow me to select media from multiple pages of the browser, and It does show the correct count, when I click submit it only inserts the media from the current page you are on in the EB.

    Am I missing something?

  • Issue was unassigned.
  • Status changed to Needs work 10 months ago
  • πŸ‡§πŸ‡ͺBelgium tim-diels Belgium πŸ‡§πŸ‡ͺ

    @loze you're right, we also experience the same issue where only the items of current page are added.

    Also getting a "Uncaught ReferenceError: _ is not defined". Seems the backbone and underscore libraries are moved to internal and shouldn't be used anymore.

    # Internal library. Do not depend on it outside core nor add new core usage.
    # The library will be removed as soon as the following issues are fixed:
    # - https://www.drupal.org/project/drupal/issues/3203920 πŸ“Œ Replace Contextual Links BackboneJS usage with VanillaJS equivalent Needs work
    # - https://www.drupal.org/project/drupal/issues/3204011 πŸ“Œ Replace Tour BackboneJS usage with VanillaJS equivalent Needs work
    # - https://www.drupal.org/project/drupal/issues/3204015 β†’

    For now the easy fix was to change the libraries to internal, but the patch needs to be rewritten to work without backbone and underscore.

    So next steps would be:

    • identify why multiple pages won't work
    • rewrite patch to work without backbone and underscore
Production build 0.69.0 2024