function File::preCreate

Same name in other branches
  1. 8.9.x core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::preCreate()
  2. 10 core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::preCreate()
  3. 11.x core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::preCreate()

Overrides EntityBase::preCreate

File

core/modules/file/src/Entity/File.php, line 155

Class

File
Defines the file entity class.

Namespace

Drupal\file\Entity

Code

public static function preCreate(EntityStorageInterface $storage, array &$values) {
    // Automatically detect filename if not set.
    if (!isset($values['filename']) && isset($values['uri'])) {
        $values['filename'] = \Drupal::service('file_system')->basename($values['uri']);
    }
    // Automatically detect filemime if not set.
    if (!isset($values['filemime']) && isset($values['uri'])) {
        $guesser = \Drupal::service('file.mime_type.guesser');
        if ($guesser instanceof MimeTypeGuesserInterface) {
            $values['filemime'] = $guesser->guessMimeType($values['uri']);
        }
        else {
            $values['filemime'] = $guesser->guess($values['uri']);
            @trigger_error('\\Symfony\\Component\\HttpFoundation\\File\\MimeType\\MimeTypeGuesserInterface is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Implement \\Symfony\\Component\\Mime\\MimeTypeGuesserInterface instead. See https://www.drupal.org/node/3133341', E_USER_DEPRECATED);
        }
    }
}

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