5fdb3ff859e18622f6af9307b109287e7f0cf0f9
[yaffs-website] / web / core / modules / node / src / Plugin / EntityReferenceSelection / NodeSelection.php
1 <?php
2
3 namespace Drupal\node\Plugin\EntityReferenceSelection;
4
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
6 use Drupal\node\NodeInterface;
7
8 /**
9  * Provides specific access control for the node entity type.
10  *
11  * @EntityReferenceSelection(
12  *   id = "default:node",
13  *   label = @Translation("Node selection"),
14  *   entity_types = {"node"},
15  *   group = "default",
16  *   weight = 1
17  * )
18  */
19 class NodeSelection extends DefaultSelection {
20
21   /**
22    * {@inheritdoc}
23    */
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);
33     }
34     return $query;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
41     $node = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
42
43     // In order to create a referenceable node, it needs to published.
44     /** @var \Drupal\node\NodeInterface $node */
45     $node->setPublished();
46
47     return $node;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
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();
60       });
61     }
62     return $entities;
63   }
64
65 }