You are here

public function View::getForm in Entity Browser 8

Same name in this branch
  1. 8 src/Plugin/EntityBrowser/Widget/View.php \Drupal\entity_browser\Plugin\EntityBrowser\Widget\View::getForm()
  2. 8 src/Plugin/EntityBrowser/SelectionDisplay/View.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\View::getForm()
Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/SelectionDisplay/View.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\View::getForm()

Returns selection display form.

Parameters

array $original_form: Entire form built up to this point. Form elements for selection display should generally not be added directly to it but returned from function as a separated unit.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

Return value

array Form structure.

Overrides SelectionDisplayInterface::getForm

File

src/Plugin/EntityBrowser/SelectionDisplay/View.php, line 38

Class

View
Displays current selection in a View.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay

Code

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

  // TODO - do we need better error handling for view and view_display
  // (in case either of those is nonexistent
  // or display not of correct type)?
  $storage =& $form_state
    ->getStorage();
  if (empty($storage['selection_display_view']) || $form_state
    ->isRebuilding()) {
    $storage['selection_display_view'] = $this->entityTypeManager
      ->getStorage('view')
      ->load($this->configuration['view'])
      ->getExecutable();
  }

  // TODO - if there are entities that are selected multiple times this
  // displays them only once. Reason for that is how SQL and Views work and
  // we probably can't do much about it.
  $selected_entities = $form_state
    ->get([
    'entity_browser',
    'selected_entities',
  ]);
  if (!empty($selected_entities)) {
    $ids = array_map(function (EntityInterface $item) {
      return $item
        ->id();
    }, $selected_entities);
    $storage['selection_display_view']
      ->setArguments([
      implode(',', $ids),
    ]);
  }
  $form['view'] = $storage['selection_display_view']
    ->executeDisplay($this->configuration['view_display']);
  $form['use_selected'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Use selection'),
    '#name' => 'use_selected',
  ];
  return $form;
}