function FileItem::getUploadValidators

Same name and namespace in other branches
  1. 11.x core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators()
  2. 10 core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators()
  3. 8.9.x core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::getUploadValidators()

Retrieves the upload validators for a file field.

Return value

array An array suitable for passing to file_save_upload() or the file field element's '#upload_validators' property.

File

core/modules/file/src/Plugin/Field/FieldType/FileItem.php, line 315

Class

FileItem
Plugin implementation of the 'file' field type.

Namespace

Drupal\file\Plugin\Field\FieldType

Code

public function getUploadValidators() {
  $validators = [];
  $settings = $this->getSettings();
  // Cap the upload size according to the PHP limit.
  $max_filesize = Bytes::toNumber(Environment::getUploadMaxSize());
  if (!empty($settings['max_filesize'])) {
    $max_filesize = min($max_filesize, Bytes::toNumber($settings['max_filesize']));
  }
  // There is always a file size limit due to the PHP server limit.
  $validators['file_validate_size'] = [
    $max_filesize,
  ];
  // Add the extension check if necessary.
  if (!empty($settings['file_extensions'])) {
    $validators['file_validate_extensions'] = [
      $settings['file_extensions'],
    ];
  }
  return $validators;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.