You are here

protected function BynderSearch::prepareEntities in Bynder 4.0.x

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 src/Plugin/EntityBrowser/Widget/BynderSearch.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::prepareEntities()
  3. 8.2 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 89

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
    ->checkType()) {
    return [];
  }
  $media = [];
  $selection = Json::decode($form_state
    ->getValue('bynder_selection', ''));
  $storage = $this->entityTypeManager
    ->getStorage('media');
  if (!$selection) {
    return [];
  }
  $image_type = NULL;
  $image_source_field = NULL;
  $source_fields = [];
  if ($this->configuration['media_type']) {

    /** @var \Drupal\media\MediaTypeInterface $image_type */
    $image_type = $this->entityTypeManager
      ->getStorage('media_type')
      ->load($this->configuration['media_type']);
    $image_source_field = $image_type
      ->getSource()
      ->getConfiguration()['source_field'];
    $source_fields[] = $image_source_field;
  }
  $document_type = NULL;
  $document_source_field = NULL;
  if ($this->configuration['media_type_document']) {

    /** @var \Drupal\media\MediaTypeInterface $document_type */
    $document_type = $this->entityTypeManager
      ->getStorage('media_type')
      ->load($this->configuration['media_type_document']);
    $document_source_field = $document_type
      ->getSource()
      ->getConfiguration()['source_field'];
    if ($document_source_field != $image_source_field) {
      $source_fields[] = $document_source_field;
    }
  }
  $video_type = NULL;
  $video_source_field = NULL;
  if ($this->configuration['media_type_video']) {

    /** @var \Drupal\media\MediaTypeInterface $video_type */
    $video_type = $this->entityTypeManager
      ->getStorage('media_type')
      ->load($this->configuration['media_type_video']);
    $video_source_field = $video_type
      ->getSource()
      ->getConfiguration()['source_field'];
    if ($video_source_field != $image_source_field) {
      $source_fields[] = $video_source_field;
    }
  }
  foreach ($selection as $bynder_info) {
    $query = $storage
      ->getQuery();
    $source_field_condition = $query
      ->orConditionGroup();
    foreach ($source_fields as $source_field) {
      $source_field_condition
        ->condition($source_field, $bynder_info['id']);
    }
    $mid = $query
      ->condition($source_field_condition)
      ->range(0, 1)
      ->execute();
    if ($mid) {
      $media[] = $storage
        ->load(reset($mid));
    }
    else {
      if ($bynder_info['type'] == 'IMAGE' && $image_type) {
        $media[] = $storage
          ->create([
          'bundle' => $image_type
            ->id(),
          $image_source_field => $bynder_info['id'],
          'name' => $bynder_info['name'],
        ]);
      }
      elseif ($bynder_info['type'] == 'DOCUMENT' && $document_type) {
        $media[] = $storage
          ->create([
          'bundle' => $document_type
            ->id(),
          $document_source_field => $bynder_info['id'],
          'name' => $bynder_info['name'],
        ]);
      }
      elseif ($bynder_info['type'] == 'VIDEO' && $video_type) {
        $media[] = $storage
          ->create([
          'bundle' => $video_type
            ->id(),
          $video_source_field => $bynder_info['id'],
          'name' => $bynder_info['name'],
        ]);
      }
    }
  }
  return $media;
}