You are here

public function DropDown::getForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/WidgetSelector/DropDown.php \Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector\DropDown::getForm()

Returns widget selector form.

Return value

array Form structure.

Overrides WidgetSelectorInterface::getForm

File

src/Plugin/EntityBrowser/WidgetSelector/DropDown.php, line 22

Class

DropDown
Displays widgets in a select list.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\WidgetSelector

Code

public function getForm(array &$form = [], FormStateInterface &$form_state = NULL) {

  // Set a wrapper container for us to replace the form on ajax call.
  $form['#prefix'] = '<div id="entity-browser-form">';
  $form['#suffix'] = '</div>';

  /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
  $browser = $form_state
    ->getFormObject()
    ->getEntityBrowser();
  $widget_ids = [];
  foreach ($this->widget_ids as $widget_id => $widget_name) {
    if ($browser
      ->getWidget($widget_id)
      ->access()
      ->isAllowed()) {
      $widget_ids[$widget_id] = $widget_name;
    }
  }
  $element['widget'] = [
    '#type' => 'select',
    '#options' => $widget_ids,
    '#default_value' => $this
      ->getDefaultWidget(),
    '#executes_submit_callback' => TRUE,
    '#limit_validation_errors' => [
      [
        'widget',
      ],
    ],
    // #limit_validation_errors only takes effect if #submit is present.
    '#submit' => [],
    '#ajax' => [
      'callback' => [
        $this,
        'changeWidgetCallback',
      ],
      'wrapper' => 'entity-browser-form',
    ],
  ];
  $element['change'] = [
    '#type' => 'submit',
    '#name' => 'change',
    '#value' => $this
      ->t('Change'),
    '#attributes' => [
      'class' => [
        'js-hide',
      ],
    ],
  ];
  return $element;
}