- 🇧🇷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 2:22pm 4 April 2023 - 🇧🇷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'sbaseFieldDefinitions()
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