3 namespace Drupal\file\Plugin\EntityReferenceSelection;
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
8 * Provides specific access control for the file entity type.
10 * @EntityReferenceSelection(
11 * id = "default:file",
12 * label = @Translation("File selection"),
13 * entity_types = {"file"},
18 class FileSelection extends DefaultSelection {
23 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
24 $query = parent::buildEntityQuery($match, $match_operator);
25 // Allow referencing :
26 // - files with status "permanent"
27 // - or files uploaded by the current user (since newly uploaded files only
28 // become "permanent" after the containing entity gets validated and
30 $query->condition($query->orConditionGroup()
31 ->condition('status', FILE_STATUS_PERMANENT)
32 ->condition('uid', $this->currentUser->id()));
39 public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
40 $file = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
42 // In order to create a referenceable file, it needs to have a "permanent"
44 /** @var \Drupal\file\FileInterface $file */
45 $file->setPermanent();
53 public function validateReferenceableNewEntities(array $entities) {
54 $entities = parent::validateReferenceableNewEntities($entities);
55 $entities = array_filter($entities, function ($file) {
56 /** @var \Drupal\file\FileInterface $file */
57 return $file->isPermanent() || $file->getOwnerId() === $this->currentUser->id();