You are here

public static function BynderUpload::batchCreateEntities in Bynder 8

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

Upload batch operation callback which creates media entities.

File

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

Class

BynderUpload
Uses upload to create media entities.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

public static function batchCreateEntities($file, $source_field, $bundle, &$context) {
  try {
    if (\Drupal::service('session')
      ->get('upload_permissions', FALSE) != 'MEDIAUPLOADFORAPPROVAL') {

      // Let's try to fetch the uploaded resource from the API as we will be
      // able to save it only if that succeeds.
      $uuid = $context['results'][$file['path']];
      \Drupal::service('bynder_api')
        ->getMediaInfo($uuid);
      $entity = Media::create([
        'bundle' => $bundle,
        $source_field => $uuid,
      ]);
      unset($context['results'][$file['path']]);
      $context['results'][] = $entity;
      $context['message'] = t('Mapped @file locally.', [
        '@file' => $file['filename'],
      ]);
    }
    else {
      drupal_set_message(t("Your file was uploaded to Bynder but needs to be approved before you can use it. Please go to your Bynder waiting room and review the uploaded assets."), 'warning');
    }
    $context['finished'] = 1;
  } catch (\Exception $e) {

    // If fetching failed try few more times. If waiting doesn't help fail the
    // batch eventually.
    if (empty($context['sandbox']['fails'])) {
      $context['sandbox']['fails'] = 0;
    }
    $context['sandbox']['fails']++;
    $context['finished'] = 0;
    $context['message'] = t('Mapping @file locally.', [
      '@file' => $file['filename'],
    ]);
    sleep(3);
    if ($context['sandbox']['fails'] >= static::FAIL_LIMIT) {
      (new UploadFailedException(t("There was an unexpected error after uploading the file to Bynder. ")))
        ->logException()
        ->displayMessage();
      drupal_set_message(t("There was an unexpected error after uploading the file to Bynder. Please contact your site administrator for more info."), 'warning');
    }
  }
}