You are here

public function MediaSelection::validateReferenceableNewEntities in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php \Drupal\media\Plugin\EntityReferenceSelection\MediaSelection::validateReferenceableNewEntities()
  2. 10 core/modules/media/src/Plugin/EntityReferenceSelection/MediaSelection.php \Drupal\media\Plugin\EntityReferenceSelection\MediaSelection::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/media/src/Plugin/EntityReferenceSelection/MediaSelection.php, line 50

Class

MediaSelection
Provides specific access control for the media entity type.

Namespace

Drupal\media\Plugin\EntityReferenceSelection

Code

public function validateReferenceableNewEntities(array $entities) {
  $entities = parent::validateReferenceableNewEntities($entities);

  // Mirror the conditions checked in buildEntityQuery().
  if (!$this->currentUser
    ->hasPermission('administer media')) {
    $entities = array_filter($entities, function ($media) {

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