class FileSizeLimitConstraintValidator

Same name and namespace in other branches
  1. 11.x core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileSizeLimitConstraintValidator

Validates the FileSizeLimitConstraint.

Hierarchy

Expanded class hierarchy of FileSizeLimitConstraintValidator

File

core/modules/file/src/Plugin/Validation/Constraint/FileSizeLimitConstraintValidator.php, line 16

Namespace

Drupal\file\Plugin\Validation\Constraint
View source
class FileSizeLimitConstraintValidator extends BaseFileConstraintValidator implements ContainerInjectionInterface {
  
  /**
   * Creates a new FileSizeConstraintValidator.
   *
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   The current user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(protected AccountInterface $currentUser, protected EntityTypeManagerInterface $entityTypeManager) {
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('current_user'), $container->get('entity_type.manager'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function validate(mixed $value, Constraint $constraint) : void {
    $file = $this->assertValueIsFile($value);
    if (!$constraint instanceof FileSizeLimitConstraint) {
      throw new UnexpectedTypeException($constraint, FileSizeLimitConstraint::class);
    }
    $fileLimit = $constraint->fileLimit;
    if ($fileLimit && $file->getSize() > $fileLimit) {
      $this->context
        ->addViolation($constraint->maxFileSizeMessage, [
        '%filesize' => ByteSizeMarkup::create($file->getSize()),
        '%maxsize' => ByteSizeMarkup::create($fileLimit),
      ]);
    }
    $userLimit = $constraint->userLimit;
    // Save a query by only calling spaceUsed() when a limit is provided.
    if ($userLimit) {
      /** @var \Drupal\file\FileStorageInterface $fileStorage */
      $fileStorage = $this->entityTypeManager
        ->getStorage('file');
      $spaceUsed = $fileStorage->spaceUsed($this->currentUser
        ->id()) + $file->getSize();
      if ($spaceUsed > $userLimit) {
        $this->context
          ->addViolation($constraint->diskQuotaMessage, [
          '%filesize' => ByteSizeMarkup::create($file->getSize()),
          '%quota' => ByteSizeMarkup::create($userLimit),
        ]);
      }
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
BaseFileConstraintValidator::assertValueIsFile protected function Checks the value is of type FileInterface.
FileSizeLimitConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FileSizeLimitConstraintValidator::validate public function
FileSizeLimitConstraintValidator::__construct public function Creates a new FileSizeConstraintValidator.

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