You are here

protected function FileUpload::validateFile in Lightning Media 8.4

Same name and namespace in other branches
  1. 8.3 src/Plugin/EntityBrowser/Widget/FileUpload.php \Drupal\lightning_media\Plugin\EntityBrowser\Widget\FileUpload::validateFile()

Validates the file entity associated with a media item.

Parameters

\Drupal\media\MediaInterface $media: The media item.

Return value

array[] Any errors returned by file_validate().

1 call to FileUpload::validateFile()
FileUpload::validate in src/Plugin/EntityBrowser/Widget/FileUpload.php

File

src/Plugin/EntityBrowser/Widget/FileUpload.php, line 138

Class

FileUpload
An Entity Browser widget for creating media entities from uploaded files.

Namespace

Drupal\lightning_media\Plugin\EntityBrowser\Widget

Code

protected function validateFile(MediaInterface $media) {
  $field = $media
    ->getSource()
    ->getSourceFieldDefinition($media->bundle->entity)
    ->getName();

  /** @var \Drupal\file\Plugin\Field\FieldType\FileItem $item */
  $item = $media
    ->get($field)
    ->first();
  $validators = [
    // It's maybe a bit overzealous to run this validator, but hey...better
    // safe than screwed over by script kiddies.
    'file_validate_name_length' => [],
  ];
  $validators = array_merge($validators, $item
    ->getUploadValidators());

  // This function is only called by the custom FileUpload widget, which runs
  // file_validate_extensions before this function. So there's no need to
  // validate the extensions again.
  unset($validators['file_validate_extensions']);

  // If this is an image field, add image validation. Against all sanity,
  // this is normally done by ImageWidget, not ImageItem, which is why we
  // need to facilitate this a bit.
  if ($item instanceof ImageItem) {

    // Validate that this is, indeed, a supported image.
    $validators['file_validate_is_image'] = [];
    $settings = $item
      ->getFieldDefinition()
      ->getSettings();
    if ($settings['max_resolution'] || $settings['min_resolution']) {
      $validators['file_validate_image_resolution'] = [
        $settings['max_resolution'],
        $settings['min_resolution'],
      ];
    }
  }
  return file_validate($item->entity, $validators);
}