3

I'm having trouble getting the entity browser widget to work correctly in a custom form. Just wondering if anyone has had any success in getting it working. I've got to the point the button is rendering, you can use the browser, select the media. The the media id is also attached to the hidden field the widget generates.

...from my digging around it looks like it never creates the right markup in the "current" div that get sent back from the ajax request that triggers on "process"

Code below...

      $form['entity_browser_wrapper'] = [
      '#type' => 'container',
      'widget' => [
          '#title' => t('Entity Browser'),
          '#field_name' => 'entity_browser_wrapper',
          '#field_parents' => [],
          '#required' => false,
          '#parents' => ['entity_browser_wrapper'],
          '#tree' => true,
          '#id' => 'edit-entity-browser-wrapper',
          '#type' => 'details',
          '#open' => true,
          'entity_browser' => [
              '#type' => 'entity_browser',
              '#entity_browser' => 'document_browser',
              '#cardinality' => 1,
              '#entity_browser_validators' => ['entity_type' => ['type' => 'media']],
              '#process' => [
                  ['\Drupal\entity_browser\Element\EntityBrowserElement', 'processEntityBrowser'],
                  ['\Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget', 'processEntityBrowser'],
              ]
          ],
          'current' => [
              '#theme_wrappers' => ['container'],
              '#attributes' => [
                  'class' => 'entities-list',
              ],
              'items' => [],
          ],
      ],
  ];

This is based off a question I found in the entity browser issues https://www.drupal.org/node/2816881. I've looked into the suggestions that were posted and have come up blank.

Thanks

  • Were you able to provide the entity browser in form ? – echo May 26 '18 at 4:52
0

I got a possibly easier solution. you need to have entity_browser and entity_browser_block required as dependencies and installed in your module. Then in your BlockBase buildForm or LayoutDefault buildConfigurationForm you can do something like what they do in the entity_browser_block module in the EntityBrowserBlock.php

// make sure you have this at the top of your file
use Drupal\entity_browser_block\Plugin\Block\EntityBrowserBlock;

// blockForm or buildConfigurationForm
$test = EntityBrowserBlock::loadEntitiesByIDs(explode(' ',$configuration['selection']['entity_browser']['entity_ids']));
$form['selection'] = [
  '#type' => 'container',
  '#attributes' => ['id' => 'entity-browser-block-form'],
];
$form['selection']['entity_browser'] = [
  '#type' => 'entity_browser',
  '#entity_browser' => 'classes',
  '#process' => [
    [
      '\Drupal\entity_browser\Element\EntityBrowserElement',
      'processEntityBrowser',
    ],
    ['\Drupal\entity_browser_block\Plugin\Block\EntityBrowserBlock', 'processEntityBrowser'],
  ],
  '#default_value' => $test,
];

$order_class = 'entity-browser-block-delta-order';

$form['selection']['table'] = [
  '#type' => 'table',
  '#header' => [
    $this->t('Title'),
    $this->t('Operations'),
  ],
  '#empty' => $this->t('No entities yet'),
  '#tabledrag' => [
    [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => $order_class,
    ],
  ],
  '#process' => [
    ['\Drupal\entity_browser_block\Plugin\Block\EntityBrowserBlock', 'processTable'],
  ],
  '#default_view_modes' => 'default',
];
| improve this answer | |

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.