3 namespace Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector;
5 use Drupal\entity_browser\WidgetSelectorBase;
6 use Drupal\Core\Form\FormStateInterface;
9 * Displays widgets in a select list.
11 * @EntityBrowserWidgetSelector(
13 * label = @Translation("Drop down widget"),
14 * description = @Translation("Displays the widgets in a drop down.")
17 class DropDown extends WidgetSelectorBase {
22 public function getForm(array &$form = [], FormStateInterface &$form_state = NULL) {
23 // Set a wrapper container for us to replace the form on ajax call.
24 $form['#prefix'] = '<div id="entity-browser-form">';
25 $form['#suffix'] = '</div>';
27 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
28 $browser = $form_state->getFormObject()->getEntityBrowser();
31 foreach ($this->widget_ids as $widget_id => $widget_name) {
32 if ($browser->getWidget($widget_id)->access()->isAllowed()) {
33 $widget_ids[$widget_id] = $widget_name;
37 $element['widget'] = [
39 '#options' => $widget_ids,
40 '#default_value' => $this->getDefaultWidget(),
41 '#executes_submit_callback' => TRUE,
42 '#limit_validation_errors' => [['widget']],
43 // #limit_validation_errors only takes effect if #submit is present.
46 'callback' => [$this, 'changeWidgetCallback'],
47 'wrapper' => 'entity-browser-form',
51 $element['change'] = [
54 '#value' => $this->t('Change'),
55 '#attributes' => ['class' => ['js-hide']],
64 public function submit(array &$form, FormStateInterface $form_state) {
65 return $form_state->getValue('widget');
69 * AJAX callback to refresh form.
73 * @param FormStateInterface $form_state
77 * Form element to replace.
79 public function changeWidgetCallback(array &$form, FormStateInterface $form_state) {