You are here

protected function View::prepareEntities in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityBrowser/Widget/View.php \Drupal\entity_browser\Plugin\EntityBrowser\Widget\View::prepareEntities()

Prepares the entities without saving them.

We need this method when we want to validate or perform other operations before submit.

Parameters

array $form: Complete form.

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

Return value

\Drupal\Core\Entity\EntityInterface[] Array of entities.

Overrides WidgetBase::prepareEntities

1 call to View::prepareEntities()
View::submit in src/Plugin/EntityBrowser/Widget/View.php
Submits form.

File

src/Plugin/EntityBrowser/Widget/View.php, line 220

Class

View
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\entity_browser\Plugin\EntityBrowser\Widget

Code

protected function prepareEntities(array $form, FormStateInterface $form_state) {
  if (is_array($form_state
    ->getUserInput()['entity_browser_select'])) {
    $selected_rows = array_values(array_filter($form_state
      ->getUserInput()['entity_browser_select']));
  }
  else {
    $selected_rows = [
      $form_state
        ->getUserInput()['entity_browser_select'],
    ];
  }
  $entities = [];
  foreach ($selected_rows as $row) {
    $item = explode(':', $row);
    if (count($item) == 2) {
      list($type, $id) = $item;
      $storage = $this->entityTypeManager
        ->getStorage($type);
      if ($entity = $storage
        ->load($id)) {
        $entities[] = $entity;
      }
    }
  }
  return $entities;
}