You are here

public function BynderSearch::submit 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::submit()
  2. 8.2 src/Plugin/EntityBrowser/Widget/BynderSearch.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::submit()
  3. 4.0.x src/Plugin/EntityBrowser/Widget/BynderSearch.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::submit()

File

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

Class

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

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

public function submit(array &$element, array &$form, FormStateInterface $form_state) {
  if (!empty($form_state
    ->getTriggeringElement()['#eb_widget_main_submit'])) {
    try {

      /*
       * We store info about media assets that we already fetched so the media
       * entity plugin can use them and avoid doing more API requests.
       *
       * @see \Drupal\bynder\Plugin\MediaEntity\Type\Bynder::getField()
       */
      $selected_ids = array_keys(array_filter($form_state
        ->getValue('selection', [])));
      if ($api_list = $form_state
        ->get('bynder_media_list')) {
        foreach ($api_list['media'] as $api_item) {
          foreach ($selected_ids as $selected_id) {
            if ($api_item['id'] == $selected_id) {
              $this->cache
                ->set('bynder_item_' . $selected_id, $api_item, $this->time
                ->getRequestTime() + 120);
            }
          }
        }
      }
      $media = $this
        ->prepareEntities($form, $form_state);
      array_walk($media, function (MediaInterface $media_item) {
        $media_item
          ->save();
      });
      $this
        ->selectEntities($media, $form_state);
    } catch (\UnexpectedValueException $e) {
      drupal_set_message($this
        ->t('Bynder integration is not configured correctly. Please contact the site administrator.'), 'error');
    }
  }
}