You are here

public function CommentSelection::validateReferenceableNewEntities in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php \Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection::validateReferenceableNewEntities()
  2. 10 core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php \Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection::validateReferenceableNewEntities()

Validates which newly created entities can be referenced.

This method should replicate the logic implemented by \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface::validateReferenceableEntities(), but applied to newly created entities that have not been saved yet.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities to check.

Return value

\Drupal\Core\Entity\EntityInterface[] The incoming $entities parameter, filtered for valid entities. Array keys are preserved.

Overrides DefaultSelection::validateReferenceableNewEntities

File

core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php, line 53

Class

CommentSelection
Provides specific access control for the comment entity type.

Namespace

Drupal\comment\Plugin\EntityReferenceSelection

Code

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 ($comment) {

      /** @var \Drupal\comment\CommentInterface $comment */
      return $comment
        ->isPublished();
    });
  }
  return $entities;
}