You are here

function s3fs_cors_form_entity_form_display_edit_form_alter in S3 File System CORS Upload 8

Implements hook_form_FORM_ID_alter().

Remove the S3 Cors widget option from file or image fields that are not using S3 storage.

File

./s3fs_cors.module, line 45
Allow uploading of files directly to AmazonS3 via the browser using CORS.

Code

function s3fs_cors_form_entity_form_display_edit_form_alter(&$form, FormStateInterface $form_state) {
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $entityManager = Drupal::service('entity_field.manager');
  $field_definitions = $entityManager
    ->getFieldDefinitions($entity_type, $bundle);
  $s3_public = Settings::get('s3fs.use_s3_for_public');
  $s3_private = Settings::get('s3fs.use_s3_for_private');
  foreach ($form['#fields'] as $field) {
    $field_definition = $field_definitions[$field];
    if ($field_definition instanceof FieldConfigInterface) {

      /** @var \Drupal\field\Entity\FieldConfig $field_definition */
      $field_type = $field_definition
        ->get('field_type');
      if ($field_type == 'file' || $field_type == 'image') {

        /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
        $field_storage = $field_definition
          ->getFieldStorageDefinition();
        $uri_scheme = $field_storage
          ->getSetting('uri_scheme');
        if ($uri_scheme == 's3' || $uri_scheme == 'public' && $s3_public || $uri_scheme == 'private' && $s3_private) {
          continue;
        }
        $widget = 's3fs_cors_' . $field_type . '_widget';
        unset($form['fields'][$field]['plugin']['type']['#options'][$widget]);
      }
    }
  }
}