3 namespace Drupal\node\Plugin\EntityReferenceSelection;
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
6 use Drupal\node\NodeInterface;
9 * Provides specific access control for the node entity type.
11 * @EntityReferenceSelection(
12 * id = "default:node",
13 * label = @Translation("Node selection"),
14 * entity_types = {"node"},
19 class NodeSelection extends DefaultSelection {
24 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
25 $query = parent::buildEntityQuery($match, $match_operator);
26 // Adding the 'node_access' tag is sadly insufficient for nodes: core
27 // requires us to also know about the concept of 'published' and
28 // 'unpublished'. We need to do that as long as there are no access control
29 // modules in use on the site. As long as one access control module is there,
30 // it is supposed to handle this check.
31 if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) {
32 $query->condition('status', NodeInterface::PUBLISHED);
40 public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
41 $node = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
43 // In order to create a referenceable node, it needs to published.
44 /** @var \Drupal\node\NodeInterface $node */
45 $node->setPublished();
53 public function validateReferenceableNewEntities(array $entities) {
54 $entities = parent::validateReferenceableNewEntities($entities);
55 // Mirror the conditions checked in buildEntityQuery().
56 if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) {
57 $entities = array_filter($entities, function ($node) {
58 /** @var \Drupal\node\NodeInterface $node */
59 return $node->isPublished();