Validate widget entity type and bundles

Created on 14 December 2017, over 6 years ago
Updated 14 June 2024, 14 days ago

Problem/Motivation

Entity browsers do not have an entity type or set of bundles. They are entity type and entity bundle agnostic. Their widgets are often specific to a certain type, such as the file upload widget, or a view widget where there is a certain entity type. Users often think they can use the file upload widget for media and the media upload widget for files. Or they might select an entity browser with a view for users on a node field, and everything works until you go to save and you get an "This entity cannot be referenced" error.

Proposed resolution

Where possible, widgets should expose their entity type and bundle type so that it can be validated against the widget context.

The widgets should be validated on the field widget settings form. If they do not match, a useful error should display to the user.

Additionally, since there may be existing configs, or someone might copy and past within a config, we should have validation when the widget loads to check that it matches the widget context. If it does not match the current context, display to the user a useful error.

A possible follow up would be to get this working on entity embed config form, for example, and inline entity form field widget config form.

Remaining tasks

- review

User interface changes

- additional error messages on the field widget config settings
- replacement of widget with error message if misconfigured within entity browser

API changes

- WidgetBase implements new interface WidgetEntityInfoInterface
- if Widgets implement its methods to expose the widget's entity type or bundle types, additional validation of the widget will take place on the field widget settings and within the entity browser.

Data model changes

- In order to add test coverage of the media_image_upload widget, adding a schema for it (which was missing).
- One thing to note on this is that the 'multiple' parameter is being set to a string even though it *should* be a boolean. This is for backwards compatibility, so as not to break existing implementations.

Original issue summary

Using Drupal 8.4.3 with the new core media module, entity_browser 2.x and media_entity_browser 2.x, when I use the 'Upload images as media items' widget and upload a new image file I get the following error:

The website encountered an unexpected error. Please try again later.
Recoverable fatal error: Argument 1 passed to file_validate() must implement interface Drupal\file\FileInterface, instance of Drupal\media\Entity\Media given, called in modules/contrib/entity_browser/src/Plugin/EntityBrowser/WidgetValidation/File.php on line 31 and defined in file_validate() (line 286 of core/modules/file/file.module).

For example: #2875209: TypeError: Argument 1 passed to file_validate() must implement interface Drupal\file\FileInterface, instance of Drupal\media_entity\Entity\Media given β†’ mentions the fix as changing the type of field, however that is not a solution to this problem since the module allows the entity browser to be used for these field types.

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Widget plugins

Created by

πŸ‡¦πŸ‡ΊAustralia rooby

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.

Production build 0.69.0 2024