You are here

protected function BynderSearch::prepareEntities in Bynder 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/EntityBrowser/Widget/BynderSearch.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::prepareEntities()
  2. 8.2 src/Plugin/EntityBrowser/Widget/BynderSearch.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::prepareEntities()
  3. 4.0.x src/Plugin/EntityBrowser/Widget/BynderSearch.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::prepareEntities()
1 call to BynderSearch::prepareEntities()
BynderSearch::submit in src/Plugin/EntityBrowser/Widget/BynderSearch.php

File

src/Plugin/EntityBrowser/Widget/BynderSearch.php, line 247

Class

BynderSearch
Uses a Bynder API to search and provide entity listing in a browser's widget.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

protected function prepareEntities(array $form, FormStateInterface $form_state) {
  if (!$this
    ->checkBundle()) {
    return [];
  }
  $media = [];
  $selected_ids = array_keys(array_filter($form_state
    ->getValue('selection', [])));

  /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
  $bundle = $this->entityTypeManager
    ->getStorage('media_bundle')
    ->load($this->configuration['media_bundle']);
  $plugin = $bundle
    ->getType();
  $source_field = $plugin
    ->getConfiguration()['source_field'];
  foreach ($selected_ids as $bynder_id) {
    $mid = $this->entityQuery
      ->get('media')
      ->condition($source_field, $bynder_id)
      ->range(0, 1)
      ->execute();
    if ($mid) {
      $media[] = $this->entityTypeManager
        ->getStorage('media')
        ->load(reset($mid));
    }
    else {
      $media[] = Media::create([
        'bundle' => $bundle
          ->id(),
        $source_field => $bynder_id,
      ]);
    }
  }
  return $media;
}