1

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:

enter image description here

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' => 'entity_browser',
      '#entity_browser' => 'media_entity_browser',
      '#title' => $this->t('Image'),
      '#default_value' => $this->configuration['image'] ?? null,
      '#weight' => '0',
    ];

    ...
}

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

enter image description here

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' => '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.

1

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.

  • Okay, I thought that might be the case - annoying! But thank you for answering. I see two choices - 1) define a content type for my custom module (in which I can use all of the form widgets I want) and then a block from which I can reference it in order to include in my layout, or 2) Roll my own JS implementation to ape that of the form widget. I think 1) will be easier for me, the developer but 2) would probably be better for an end user. – Gruffy Feb 23 '18 at 13:58
  • 1
    or 3) Define a custom entity type and a block plugin with a block form to select the custom entity. – 4k4 Feb 23 '18 at 14:49
  • 1
    Config pages is worth looking into as an alternative to creating a content type. – sonfd Mar 30 '18 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.