Hi.
I'm trying to use Entity browser for selecting of the field in node with a help of EB widgets.

I'm using Entity browser, provided by entity_browser image module (Image browser).

When I'm selecting an image in the modal - error is appeared.

The same issue I'm expecting, when I'm trying to use this module on the fresh simlytest.me instance.
Error at symplytest.me:

Error at my project:

Entity type that is expected in code: media. Entity that is in the input entity: file

Comments

anpolimus created an issue. See original summary.

anpolimus’s picture

Issue summary: View changes
anpolimus’s picture

Issue summary: View changes
anpolimus’s picture

Component: Selection display plugins » Widget plugins
marcoscano’s picture

Could you please provide more detailed steps on how to reproduce the error?
(Specially which modules are used, and what configuration is in the entity browsers. I cannot find any "Entity browser image" module, so I'm not quite sure how to reproduce your scenario).

Also, it could be helpful if you could provide an export of your entity browser configuration, if you know how to do so :) (on development -> synchronization -> export -> single item)

anpolimus’s picture

Hi.
I'm installing lightning profile at the simlpytest.me project.

anpolimus’s picture

I'll provide more detailed test case soon

slashrsm’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +D8Media
Jonah Fenn’s picture

StatusFileSize
new77.52 KB

I've run into a similar problem as well. @anpolimums, I'm assuming that you're trying to upload media into an image or file field (this was the case for me). These fields need a file entity type.

A media field is actually an entity reference - this isn't really very clearly documented yet. Here's how you add the field to your node types:

When adding a new field that will contain media types, in the Entity Reference section, select "Other" and then make your media specific configurations on the screens that follow.

I've attached a screenshot to help clarify. I hope it helps you out.

anpolimus’s picture

Hi, that was mostly configuration problem
@fenns is right - I was trying to upload image to the file field that is not media entity.
This issue could be closed.

anpolimus’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
lpeabody’s picture

I just ran into this while trying to upload new files in a CKEditor wysiwyg. Specifically I get the "The entity must be of type media." error.

lpeabody’s picture

Status: Closed (works as designed) » Active
slashrsm’s picture

Status: Active » Closed (works as designed)

As it was already mentioned #10 this shows that there is probably some mis-configuration. It is your responsibility to understand entities of which type will be provided by the widgets that are used on the Entity browser. The type of the entities returned from the widgets should match the type that is expected from the calling code.

It would be nice to have automatic checking for that, but that would be very hard to do with the current architecture that is trying not to assume anything about the type.

vlad.dancer’s picture

@slashrsm

It would be nice to have automatic checking for that

Completely agree and I think this is quite easy to achive, we need just "field instance" context inside widgets, see below.

@anpolimus despite of that fact that this is a field widget configuration question
actualy we could improve expirience using of Entity Browser (EB).

We might want to restrict running of particular EB widget if widget designed to work only with images or pdf files for instance.
All that we need is to get "field" context to extract target entity type bundles.

It would be nice to extend \Drupal\entity_browser\WidgetInterface,
and add something like isApplicable($field_instance) method to decide if a current field supports image files for instance.

But there is no "field" context neither inside a WidgetBase nor inside an EntityBrowserForm.
Actually there is basic support of "widget context" inside \Drupal\entity_browser\Form\EntityBrowserForm::init
via $form_state->set(['entity_browser', 'widget_context'], []);

We used form_state in IntelligenceBank DAM Connector project to add desired context via hook_field_widget_WIDGET_TYPE_form_alter
and later inside our EB widget to make decision what kind of media types we can create for current media reference field:

...
public function getForm(...) {
  ...
  $widget_context = $form_state->get(['entity_browser', 'widget_context']);
  $target_bundles  = array_keys($widget_context[WIDGET_KEY]['allowed_media_types']);
  
  if (empty($target_bundles)) {
    $this->showEmptyTargetBundlesError();
    return $form;
  }
  ..
  return $form;
}

real example

For me having an "isApplicable" method inside WidgetInterface - looks like this is very usefull to to allow widget to make decision on runtime if it could be used in current context.

What do you think guys?

oknate’s picture

I'm working on validation for the widgets against the target entity type, see #2930677: Validate widget entity type and bundles.

oknate’s picture

Status: Closed (works as designed) » Needs work

I have been working validating the widgets in the related issue #2930677: Validate widget entity type and bundles. Reopening to see if I can drum up a review.