diff --git a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionInterface.php b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionInterface.php index d79833f..564b673 100644 --- a/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityReferenceSelection/SelectionInterface.php @@ -38,7 +38,7 @@ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTA public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS'); /** - * Validates that existing entities can be referenced. + * Validates which existing entities can be referenced. * * @return array * An array of valid entity IDs. @@ -46,9 +46,7 @@ public function countReferenceableEntities($match = NULL, $match_operator = 'CON public function validateReferenceableEntities(array $ids); /** - * Validates that entities can be referenced. - * - * @todo naming... + * Validates which newly created entities can be referenced. * * This method should replicate the logic implemented by * validateReferenceableEntities(), but applied to newly created entities that diff --git a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraint.php b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraint.php index 3178e7a..f27c22d 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraint.php +++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraint.php @@ -40,7 +40,7 @@ class ValidReferenceConstraint extends Constraint { * * @var string */ - public $invalidAutocreateMessage = 'This entity (%type: %label)cannot be referenced.'; + public $invalidAutocreateMessage = 'This entity (%type: %label) cannot be referenced.'; /** * Violation message when the target_id is empty. diff --git a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php index c8bc4a7..2761092 100644 --- a/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php +++ b/core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php @@ -42,6 +42,20 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') /** * {@inheritdoc} */ + public function validateReferenceableNewEntities(array $entities) { + $entities = parent::validateReferenceableNewEntities($entities); + // Mirror the conditions checked in buildEntityQuery(). + if (!$this->currentUser->hasPermission('administer comments')) { + $entities = array_filter($entities, function ($entity) { + return $entity->status->value === CommentInterface::PUBLISHED; + }); + } + return $entities; + } + + /** + * {@inheritdoc} + */ public function entityQueryAlter(SelectInterface $query) { $tables = $query->getTables(); $data_table = 'comment_field_data'; diff --git a/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php b/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php index 26a3eb3..232c741 100644 --- a/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php +++ b/core/modules/file/src/Plugin/EntityReferenceSelection/FileSelection.php @@ -38,4 +38,15 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') return $query; } + /** + * {@inheritdoc} + */ + 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(); + }); + return $entities; + } + } diff --git a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php index 276c795..e707e79 100644 --- a/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php +++ b/core/modules/node/src/Plugin/EntityReferenceSelection/NodeSelection.php @@ -48,4 +48,18 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') return $query; } + /** + * {@inheritdoc} + */ + public function validateReferenceableNewEntities(array $entities) { + $entities = parent::validateReferenceableNewEntities($entities); + // Mirror the conditions checked in buildEntityQuery(). + if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) { + $entities = array_filter($entities, function ($entity) { + return $entity->status->value === NODE_PUBLISHED; + }); + } + return $entities; + } + } diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php index 8e64386..aa49b84 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php @@ -113,7 +113,7 @@ public function testEntityReferenceFieldValidation() { $entity->{$this->fieldName}->target_id = $referenced_entity->id(); $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 1, 'Validation throws a violation.'); - $this->assertEqual($violations[0]->getMessage(), t('The entity must be of bundle %bundle.', array('%bundle' => $this->bundle))); + $this->assertEqual($violations[0]->getMessage(), t('This entity (%type: %id) cannot be referenced.', array('%type' => $this->referencedEntityType, '%id' => $referenced_entity->id()))); } /** diff --git a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php index 160c50e..c830602 100644 --- a/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php +++ b/core/modules/user/src/Plugin/EntityReferenceSelection/UserSelection.php @@ -171,6 +171,25 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') /** * {@inheritdoc} */ + public function validateReferenceableNewEntities(array $entities) { + $entities = parent::validateReferenceableNewEntities($entities); + // Mirror the conditions checked in buildEntityQuery(). + if (!empty($this->configuration['handler_settings']['filter']['role'])) { + $entities = array_filter($entities, function ($entity) { + return !empty(array_intersect($entity->getRoles(), $this->configuration['handler_settings']['filter']['role'])); + }); + } + if (!$this->currentUser->hasPermission('administer users')) { + $entities = array_filter($entities, function ($entity) { + return $entity->status->value === 1; + }); + } + return $entities; + } + + /** + * {@inheritdoc} + */ public function entityQueryAlter(SelectInterface $query) { // Bail out early if we do not need to match the Anonymous user. $handler_settings = $this->configuration['handler_settings']; diff --git a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php index 8f8e8a1..af44db1 100644 --- a/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php +++ b/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php @@ -263,7 +263,8 @@ public function validateReferenceableEntities(array $ids) { * {@inheritdoc} */ public function validateReferenceableNewEntities(array $entities) { - // @todo adjust + // ViewsSelection does not supprt autocreate, there is no way to check that + // an unsaved entity will match a View once it gets saved. return []; }