You are here

public static function S3fsCorsFile::validateManagedFile in S3 File System CORS Upload 8

Render API callback: Custom validation for the managed_file element.

Copied from \Drupal\file\Element\ManagedFile. Currently identical.

Overrides ManagedFile::validateManagedFile

File

src/Element/S3fsCorsFile.php, line 354

Class

S3fsCorsFile
Provides an S3fs Cors File Element.

Namespace

Drupal\s3fs_cors\Element

Code

public static function validateManagedFile(&$element, FormStateInterface $form_state, &$complete_form) {
  $clicked_button = end($form_state
    ->getTriggeringElement()['#parents']);
  if ($clicked_button != 'remove_button' && !empty($element['fids']['#value'])) {
    $fids = $element['fids']['#value'];
    foreach ($fids as $fid) {
      if ($file = File::load($fid)) {

        // If referencing an existing file, only allow if there are existing
        // references. This prevents unmanaged files from being deleted if
        // this item were to be deleted. When files that are no longer in use
        // are automatically marked as temporary (now disabled by default),
        // it is not safe to reference a permanent file without usage. Adding
        // a usage and then later on removing it again would delete the file,
        // but it is unknown if and where it is currently referenced. However,
        // when files are not marked temporary (and then removed)
        // automatically, it is safe to add and remove usages, as it would
        // simply return to the current state.
        // @see https://www.drupal.org/node/2891902
        if ($file
          ->isPermanent() && \Drupal::config('file.settings')
          ->get('make_unused_managed_files_temporary')) {
          $references = static::fileUsage()
            ->listUsage($file);
          if (empty($references)) {

            // We expect the field name placeholder value to be wrapped in t()
            // here, so it won't be escaped again as it's already marked safe.
            $form_state
              ->setError($element, t('The file used in the @name field may not be referenced.', [
              '@name' => $element['#title'],
            ]));
          }
        }
      }
      else {

        // We expect the field name placeholder value to be wrapped in t()
        // here, so it won't be escaped again as it's already marked safe.
        $form_state
          ->setError($element, t('The file referenced by the @name field does not exist.', [
          '@name' => $element['#title'],
        ]));
      }
    }
  }

  // Check required property based on the FID.
  if ($element['#required'] && empty($element['fids']['#value']) && !in_array($clicked_button, [
    'upload_button',
    'remove_button',
  ])) {

    // We expect the field name placeholder value to be wrapped in t()
    // here, so it won't be escaped again as it's already marked safe.
    $form_state
      ->setError($element, t('@name field is required.', [
      '@name' => $element['#title'],
    ]));
  }

  // Consolidate the array value of this field to array of FIDs.
  if (!$element['#extended']) {
    $form_state
      ->setValueForElement($element, $element['fids']['#value']);
  }
}