CardinalityMediaLibraryWidget does not work with base field

Created on 11 April 2022, over 2 years ago
Updated 6 March 2024, 9 months ago

Problem/Motivation

The CardinalityMediaLibraryWidget does not work with base fields , because base field do not implement getThirdPartySetting method. This leads to this error when using a base field together with media library widget and field config cardinality module:

Error: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::getThirdPartySetting() in Drupal\field_config_cardinality\Plugin\Field\FieldWidget\CardinalityMediaLibraryWidget->formElement() (line 15 of modules/contrib/field_config_cardinality/src/Plugin/Field/FieldWidget/CardinalityMediaLibraryWidget.php).

This is related to #3117908: Cardinality for MediaLibraryWidget .

Steps to reproduce

  • Add a base field for a media entity reference to an entity
  • use the media library widget on the base field
  • Try to edit the entity

Proposed resolution

Check whether the field is an instance of FieldConfigInterface to verify that the getThirdPartySettings method exists. It also seems, that the code of the CardinalityMediaLibraryWidget could be refactored a bit to rely more on the parent class MediaLibraryWidget.

Remaining tasks

create a patch

User interface changes

none

API changes

none

Data model changes

none

🐛 Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

🇩🇪Germany stefan.korn Jossgrund

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.

  • 🇧🇷Brazil halth Brazil

    Maybe I'm not following this... But here I go:

    As far as I know, a media library field widget should only be available to fields of type media / entity reference.

    I don't recall Drupal having base fields of type media / entity reference (Unless you create your own, of course).

    Are you talking about this use case, specifically: Custom base fields of type media / entity reference?

    Otherwise I cannot see how you would use a media library widget with a base field of a type other than media / entity reference.

    Having said that, I haven't been able to reproduce the issue.

    I would appreciate if you could provide more detailed instructions.

  • Status changed to RTBC over 1 year ago
  • 🇧🇷Brazil halth Brazil

    Okay, I'm sorry about the confusion.

    I did a test in here and I can see that creating a custom base field on a custom entity is the use case you were referring to.

    I can confirm that 1) I've been able to reproduce the error by following the instructions on the issue's description; 2) The patch on #3 by @stefan.korn did fix the error for me.

    For other folks wanting to test this out, I suggest creating a custom content entity using drush gen content-entity and then using this sample field definition on your entity's baseFieldDefinitions() method:

          $fields['media_reference'] = BaseFieldDefinition::create('entity_reference')
          ->setLabel(t('Media Reference'))
          ->setDescription(t('Example field'))
          ->setSetting('target_type', 'media')
          ->setDisplayOptions('view', [
            'label' => 'above',
            'type' => 'entity_reference_label',
            'weight' => -4,
          ])
          ->setDisplayOptions('form', [
            'type' => 'media_library_widget',
            'weight' => -4,
            'settings' => [],
          ])
          ->setCardinality(3);
    
  • 🇬🇧United Kingdom hebl

    Hey,

    I've re-rolled the patch in #3 to add back the type casting to the $cardinality variable.

    This is needed to stop a failure in type comparison on line 473 of /core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php.

    Thanks

    Luke

Production build 0.71.5 2024