You are here

public function BynderUpload::submit in Bynder 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/EntityBrowser/Widget/BynderUpload.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderUpload::submit()
  2. 8 src/Plugin/EntityBrowser/Widget/BynderUpload.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderUpload::submit()
  3. 8.2 src/Plugin/EntityBrowser/Widget/BynderUpload.php \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderUpload::submit()

File

src/Plugin/EntityBrowser/Widget/BynderUpload.php, line 221

Class

BynderUpload
Uses upload to create media entities.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

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

    /** @var \Drupal\media\MediaTypeInterface $type */
    $type = $this->entityTypeManager
      ->getStorage('media_type')
      ->load($this->configuration['media_type']);
    if ($type && $type
      ->getSource() instanceof Bynder) {
      $form_state
        ->setRebuild();
      $batch = [
        'title' => $this
          ->t('Uploading assets to Bynder'),
        'init_message' => $this
          ->t('Initializing upload.'),
        'progress_message' => $this
          ->t('Processing (@percentage)...'),
        'operations' => [],
        'finished' => [
          static::class,
          'batchFinish',
        ],
      ];
      foreach ((array) $form_state
        ->getValue([
        'upload',
        'uploaded_files',
      ], []) as $file) {
        $batch['operations'][] = [
          [
            static::class,
            'batchUploadFiles',
          ],
          [
            $file,
            $this->configuration['brand'],
            $form_state
              ->getValue('description', ''),
            $this->configuration['tags'],
            $this->configuration['metaproperty_options'],
          ],
        ];
      }
      foreach ((array) $form_state
        ->getValue([
        'upload',
        'uploaded_files',
      ], []) as $file) {
        $batch['operations'][] = [
          [
            static::class,
            'batchCreateEntities',
          ],
          [
            $file,
            $type
              ->get('source_configuration')['source_field'],
            $type
              ->id(),
          ],
        ];
      }

      // Batch redirect callback needs UUID so we save it into the session.
      $this->session
        ->set('bynder_upload_batch_uuid', $form_state
        ->get([
        'entity_browser',
        'instance_uuid',
      ]));
      batch_set($batch);

      // Now that the batch is set manually set source URL which will ensure
      // that we persist all needed query arguments when redirected back to
      // the form.
      if (\Drupal::request()->query
        ->count()) {
        $batch =& batch_get();
        $source_url = Url::fromRouteMatch(\Drupal::routeMatch());
        $source_url
          ->setOption('query', \Drupal::request()->query
          ->all());
        $batch['source_url'] = $source_url;
      }
    }
    else {
      if (!$type) {
        (new BundleNotExistException($this->configuration['media_type']))
          ->logException()
          ->displayMessage();
      }
      else {
        (new BundleNotBynderException($type
          ->label()))
          ->logException()
          ->displayMessage();
      }
    }
  }
  elseif (!empty($form_state
    ->getTriggeringElement()['#eb_widget_main_submit'])) {
    try {
      $media = $this
        ->prepareEntities($form, $form_state);
      array_walk($media, function (MediaInterface $item) {
        if (!$item
          ->id()) {

          // Some race conditions might occur in some circumstances and could
          // try to save this entity twice.
          try {
            $item
              ->save();
          } catch (EntityStorageException $e) {
          }
        }
      });
      $this
        ->selectEntities($media, $form_state);
      $form_state
        ->set('uploaded_assets', NULL);
      $this
        ->clearFormValues($element, $form_state);
    } catch (BynderException $e) {
      $e
        ->displayMessage();
      return;
    }
  }
}