Problem/Motivation
when an invalid image is selected within the EntityEmbedDialog, there's no specific warning to that effect. The "image:image" display is simply removed from the list of display plugins, and if it's the only one you receive this error:

This is confusing, and it took me a while to figure to out what is going on.
Steps to recreate the bug manually.
1 Create entity embed button with "File" entity selected and "Image" display plugin selected.
langcode: en
status: true
dependencies:
module:
- entity_embed
- file
- image
label: 'Image Embed'
id: image_embed
type_id: entity
type_settings:
entity_type: file
bundles: { }
display_plugins:
- 'image:image'
entity_browser: ''
entity_browser_settings:
display_review: false
icon_uuid: null
icon_path: null
2) Try to embed through a button in the wysiwyg.
Expected, If a valid image is selected, EntityEmbedDialog moves to the "Embed" step. If an invalid image is selected, an error message is displayed, "This entity is not a valid image. Please select another entity."
Actual, Error appears: "No display options available for the selected entity. Please select another entity."
Steps to recreate the issue in code:
use Drupal\file\Entity\File;
file_put_contents('public://llama.jpg', '');
$image = File::create(['uri' => 'public://llama.jpg']);
$image->save();
$contexts = [
'entity' => $image,
'entity_type' => 'file',
];
$plugins = \Drupal::service('plugin.manager.entity_embed.display')->getDefinitionsForContexts($contexts);
dump($plugins); die();
Expected, various image display plugins should be present, but aren't.

This is despite the admin form having allowed me to select the "image:image" plugin.
Proposed resolution
A more specific message should be displayed when an invalid image is selected when using the ImageFieldFormatter display plugin.
Remaining tasks
- reviews
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | 3022754-20.patch | 10.68 KB | wim leers |
| #20 | interdiff.txt | 5.55 KB | wim leers |
| #16 | entity-embed-invalid-image-3022754-16.patch | 8.65 KB | oknate |
Comments
Comment #2
oknateComment #3
oknateComment #4
oknateComment #5
oknateComment #6
oknateComment #7
oknateComment #8
oknateSeems to be a bug in ImageFieldFormatter::access().
Comment #9
oknateOK, I think this whole issue is a little off.
It turns out if the image isn't valid, instead of telling you the image isn't valid, it tells you that no display plugins are available.
It would make more sense to display an error message "The selected image isn't valid."
Comment #10
oknateComment #11
oknateComment #12
oknateAn additional strange thing is that EntityEmbedDisplayManager::getDefinitionsForContexts() iterates through all available plugins and calls their access function when in the context of the EntityEmbedDialog we have an embed button with a list of available plugins.
In my case that was 162 different plugins! When I only had one on the list of available for my embed button!
Oh, on further inspection, it pulls them from cached definitions (DefaultPluginManager::getDefinitions) and allows other modules to alter them, etc. so that's all good, but it still calls the access function on each one, which is quite slow. We can prefilter them in the context of having a shorter list from the embed button's type settings.
I added a separate issue for this: #3022990: EntityEmbedDisplayManager::getDefinitionsForContexts checks access on irrelevant plugins
Comment #13
oknateComment #14
oknateHere's a patch that adds an error message that the image is invalid.
Comment #15
oknateComment #16
oknateAdding test coverage.
Comment #17
oknateComment #18
oknateComment #19
oknateComment #20
wim leersWhat even is "an invalid image"? This is an area of Entity Embed that still frightens me; I generally don't dare commit things in this area.
However, per point 1 below, the actual problem and fix are obvious once looking at the code. Plus additional test coverage is better than the status quo. So I'm going ahead and committing this :)
Nit: should be
$is_valid_imageper Drupal coding standards.Nit: should inject messenger.
Note: using messenger directly instead of a validation error is problematic, but that's a problem deeper in the Entity Embed's architecture, this pragmatically improves usability in avoiding mysterious errors without any actionable user feedback at all. So it's a step forward.
This is not quite the truth :) But since this is testing the behavior of a button, it's fine! The comment is just inaccurate. Removing it.
👍
Comment #21
wim leersComment #22
oknateSweet, thanks for the update. The interdiff looks good.
I used the language "invalid image" based on:
So it's very specific to imageFactory.
Comment #24
wim leers👍