You are here

public static function BynderUpload::batchUploadFiles in Bynder 8.2

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

Upload batch operation callback which uploads assets to Bynder.

File

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

Class

BynderUpload
Uses upload to create media entities.

Namespace

Drupal\bynder\Plugin\EntityBrowser\Widget

Code

public static function batchUploadFiles($file, $brand, $description, $tags, $metaproperty_options, &$context) {
  try {

    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $data = [
      'filePath' => $file_system
        ->realpath($file['path']),
      'brandId' => $brand,
      'name' => $file['filename'],
    ];
    if ($description) {
      $data['description'] = $description;
    }
    if ($tags) {
      $data['tags'] = implode(',', $tags);
    }
    if ($metaproperty_options) {
      foreach ($metaproperty_options as $metaproperty => $options) {
        $data['metaproperty.' . $metaproperty] = implode(',', $options);
      }
    }
    if (isset($context['results']['accessRequestId'])) {
      $data['accessRequestId'] = $context['results']['accessRequestId'];
    }
    $result = \Drupal::service('bynder_api')
      ->uploadFileAsync($data);
    $context['results']['accessRequestId'] = $result['accessRequestId'];
    $context['results'][$file['path']] = $result['mediaid'];
    $file_system
      ->delete($file['path']);
    $context['message'] = t('Uploaded @file to Bynder.', [
      '@file' => $file['filename'],
    ]);
  } 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('Uploading @file to Bynder.', [
      '@file' => $file['filename'],
    ]);
    if ($context['sandbox']['fails'] >= static::FAIL_LIMIT) {
      throw $e;
    }
  }
}