You are here

public function CommentSelection::validateReferenceableNewEntities in Comment Permissions 8

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 CommentSelection::validateReferenceableNewEntities

File

src/Plugin/EntityReferenceSelection/CommentSelection.php, line 39

Class

CommentSelection
Overrides CommentSelection plugin.

Namespace

Drupal\comment_perm\Plugin\EntityReferenceSelection

Code

public function validateReferenceableNewEntities(array $entities) {

  // Default selection validation by bundle.
  $entities = array_filter($entities, function ($entity) {
    $target_bundles = $this
      ->getConfiguration()['target_bundles'];
    if (isset($target_bundles)) {
      return in_array($entity
        ->bundle(), $target_bundles);
    }
    return TRUE;
  });

  // Mirror the conditions checked in buildEntityQuery().
  if (!$this
    ->userHasAdminPerm()) {
    $entities = array_filter($entities, function ($comment) {

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