You are here

public static function S3CorsManagedFileHelper::preProcessCors in Flysystem - S3 8

Same name and namespace in other branches
  1. 2.0.x src/S3CorsManagedFileHelper.php \Drupal\flysystem_s3\S3CorsManagedFileHelper::preProcessCors()

Function preProcessCors prepare the field to use CORS upload.

File

src/S3CorsManagedFileHelper.php, line 23

Class

S3CorsManagedFileHelper
Helper for altering and processing a managed_file element for CORS upload.

Namespace

Drupal\flysystem_s3

Code

public static function preProcessCors(array &$element) {
  if (isset($element['#s3_cors']) && !$element['#s3_cors']) {

    // S3 CORS support has been specifically disabled for this element.
    return $element;
  }

  // Default to off until the upload destination is confirmed to be an S3
  // scheme, CORS support is enabled in the flysystem config, and the user
  // has permission to upload files using CORS.
  $element['#s3_cors'] = FALSE;
  if (!empty($element['#upload_location']) && ($scheme = \Drupal::service('file_system')
    ->uriScheme($element['#upload_location']))) {
    if (static::isCorsAvailable($scheme)) {

      // @todo Verify account permission/role respected with cache tags.
      // Disable the default progress indicator.
      $element['#progress_indicator'] = 'none';

      // Add a flag to the element to indicate that this is a CORS upload.
      $element['#s3_cors'] = TRUE;

      // Attach the JS library to the element conditionally.
      $element['#attached']['library'][] = 'flysystem_s3/drupal.s3_cors_upload';

      // Set the default S3 ACL if it is not already set.
      if (!isset($element['#s3_acl'])) {
        $element['#s3_acl'] = static::getAcl($scheme);
      }
    }
  }
  return $element;
}