diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php index f5696cd..f228bbc 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php @@ -8,9 +8,9 @@ namespace Drupal\Core\Entity\Plugin\Validation\Constraint; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface; use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -28,23 +28,23 @@ class ValidReferenceConstraintValidator extends ConstraintValidator implements C protected $selectionManager; /** - * The entity manager. + * The entity type manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityManager; + protected $entityTypeManager; /** * Constructs a ValidReferenceConstraintValidator object. * * @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager * The selection plugin manager. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. */ - public function __construct(SelectionPluginManagerInterface $selection_manager, EntityManagerInterface $entity_manager) { + public function __construct(SelectionPluginManagerInterface $selection_manager, EntityTypeManagerInterface $entity_type_manager) { $this->selectionManager = $selection_manager; - $this->entityManager = $entity_manager; + $this->entityTypeManager = $entity_type_manager; } /** @@ -53,7 +53,7 @@ public function __construct(SelectionPluginManagerInterface $selection_manager, public static function create(ContainerInterface $container) { return new static( $container->get('plugin.manager.entity_reference_selection'), - $container->get('entity.manager') + $container->get('entity_type.manager') ); } @@ -128,8 +128,8 @@ public function validate($value, Constraint $constraint) { if ($invalid_target_ids = array_diff($target_ids, $valid_target_ids)) { // For accuracy of the error message, differentiate non-referenceable // and non-existent entities. - $target_type = $this->entityManager->getDefinition($target_type_id); - $existing_ids = $this->entityManager->getStorage($target_type_id)->getQuery() + $target_type = $this->entityTypeManager->getDefinition($target_type_id); + $existing_ids = $this->entityTypeManager->getStorage($target_type_id)->getQuery() ->condition($target_type->getKey('id'), $invalid_target_ids, 'IN') ->execute(); foreach ($invalid_target_ids as $delta => $target_id) { diff --git a/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php index e558e58..cd221d2 100644 --- a/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php @@ -20,7 +20,7 @@ class EntityReferenceFieldItemList extends FieldItemList implements EntityRefere */ public function getConstraints() { $constraints = parent::getConstraints(); - $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager(); + $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager(); $constraints[] = $constraint_manager->create('ValidReference', []); return $constraints; } diff --git a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php index ea89c9e..4f207e6 100644 --- a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php +++ b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php @@ -43,12 +43,13 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') * {@inheritdoc} */ public function createNewEntity($entity_type_id, $bundle, $label, $uid) { - $entity = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); + $comment = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); // In order to create a referenceable comment, it needs to published. - $entity->status->value = CommentInterface::PUBLISHED; + /** @var \Drupal\comment\CommentInterface $comment */ + $comment->setPublished(TRUE); - return $entity; + return $comment; } /** diff --git a/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php b/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php index abd06da..01fb8cd 100644 --- a/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php +++ b/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php @@ -42,13 +42,14 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') * {@inheritdoc} */ public function createNewEntity($entity_type_id, $bundle, $label, $uid) { - $entity = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); + $file = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); // In order to create a referenceable file, it needs to have a "permanent" // status. - $entity->status->value = FILE_STATUS_PERMANENT; + /** @var \Drupal\file\FileInterface $file */ + $file->setPermanent(); - return $entity; + return $file; } /** @@ -56,8 +57,9 @@ public function createNewEntity($entity_type_id, $bundle, $label, $uid) { */ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); - $entities = array_filter($entities, function ($entity) { - return $entity->status->value === FILE_STATUS_PERMANENT || $entity->uid->target_id === $this->currentUser->id(); + $entities = array_filter($entities, function ($file) { + /** @var \Drupal\file\FileInterface $file */ + return $file->isPermanent() || $file->getOwnerId() === $this->currentUser->id(); }); return $entities; } diff --git a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php index cc83b23..fbfcbd1 100644 --- a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php +++ b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php @@ -47,6 +47,7 @@ protected function setUp() { $this->user = User::create([ 'name' => 'username', + 'status' => 1, ]); $this->user->save(); } @@ -90,6 +91,7 @@ public function testFileValidationConstraint($file_type) { $file = File::create([ 'uri' => 'vfs://drupal_root/sites/default/files/test.txt', ]); + $file->setPermanent(); $file->save(); $entity_test = EntityTest::create([ diff --git a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php index 5d7954a..0055a4a 100644 --- a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php +++ b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php @@ -52,12 +52,13 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') * {@inheritdoc} */ public function createNewEntity($entity_type_id, $bundle, $label, $uid) { - $entity = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); + $node = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); // In order to create a referenceable node, it needs to published. - $entity->status->value = NODE_PUBLISHED; + /** @var \Drupal\node\NodeInterface $node */ + $node->setPublished(TRUE); - return $entity; + return $node; } /** diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index c830602..318a36e 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -171,6 +171,21 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') /** * {@inheritdoc} */ + public function createNewEntity($entity_type_id, $bundle, $label, $uid) { + $user = parent::createNewEntity($entity_type_id, $bundle, $label, $uid); + + // In order to create a referenceable user, it needs to be active. + if (!$this->currentUser->hasPermission('administer users')) { + /** @var \Drupal\user\UserInterface $user */ + $user->activate(); + } + + return $user; + } + + /** + * {@inheritdoc} + */ public function validateReferenceableNewEntities(array $entities) { $entities = parent::validateReferenceableNewEntities($entities); // Mirror the conditions checked in buildEntityQuery().