8.4 - Use entity browser in a module block from in same way as I would in a custom block I am having trouble adding an image picker to a block form defined in a custom module.

If I create a custom block type I can define an image field as entity_reference and in the block's form view tell it to use entity_browser and media_entity_browser.  This provides me with an picker with which I can select my image:



I am trying to recreate this in a block provided by my custom module, without much success

// /src/Plugin/Block/MyCustomBlock.php

function blockForm($form, FormStateInterface $form_state) 
{
    ...

    $form['image'] = [
      '#type' =&gt; 'entity_browser',
      '#entity_browser' =&gt; 'media_entity_browser',
      '#title' =&gt; $this-&gt;t('Image'),
      '#default_value' =&gt; $this-&gt;configuration['image'] ?? null,
      '#weight' =&gt; '0',
    ];

    ...
}


This gives me the following.  Note the lack of the bounding box, title and option to edit remove items: 



Exporting the configuration files for the working form view gets me this:

// core.entity_form_display.block_content.lead_block.default.yml    

...

field_image:
    weight: 1
    settings:
      entity_browser: media_entity_browser
      field_widget_display: rendered_entity
      field_widget_edit: true
      field_widget_remove: true
      selection_mode: selection_append
      field_widget_display_settings:
        view_mode: default
      open: false
    third_party_settings: {  }
    type: entity_browser_entity_reference
    region: content

 ...


But if I try and use 'type' =&gt; 'entity_browser_entity_reference' in my blockForm function the field disappears altogether.

Looking into various core files I can see that entity_browser is a @FormElement and entity_browser_entity_reference is a @FieldWidget but I don't really understand the difference.

As you might guess I am fairly new to Drupal and I feel like I am missing something fundamental.  I (think I) understand that custom block types are actually nodes internally and that they are subject to different rules to blocks which I define in my custom modules, but I don't fully grasp what the differences here are.

Is is it possible to use the entity browser in my custom module block form in the same way as it is used in the form which I build through the admin's custom block type builder?

Thank you.
No, this is not possible. Block forms in a block plugin are configuration forms and here you can only use Form API fields.


  I (think I) understand that custom block types are actually nodes
  internally and that they are subject to different rules to blocks
  which I define in my custom modules, but I don't fully grasp what the
  differences here are.


Custom blocks are not exactly nodes internally, it's very similar, custom blocks and nodes are both content entities. This doesn't change the rest of what you found out correctly. You can use entity fields and their form elements, widget and formatters only for content entities.