Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node\Plugin\EntityReferenceSelection;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
|
Chris@0
|
6 use Drupal\node\NodeInterface;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides specific access control for the node entity type.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @EntityReferenceSelection(
|
Chris@0
|
12 * id = "default:node",
|
Chris@0
|
13 * label = @Translation("Node selection"),
|
Chris@0
|
14 * entity_types = {"node"},
|
Chris@0
|
15 * group = "default",
|
Chris@0
|
16 * weight = 1
|
Chris@0
|
17 * )
|
Chris@0
|
18 */
|
Chris@0
|
19 class NodeSelection extends DefaultSelection {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * {@inheritdoc}
|
Chris@0
|
23 */
|
Chris@0
|
24 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
|
Chris@0
|
25 $query = parent::buildEntityQuery($match, $match_operator);
|
Chris@0
|
26 // Adding the 'node_access' tag is sadly insufficient for nodes: core
|
Chris@0
|
27 // requires us to also know about the concept of 'published' and
|
Chris@0
|
28 // 'unpublished'. We need to do that as long as there are no access control
|
Chris@0
|
29 // modules in use on the site. As long as one access control module is there,
|
Chris@0
|
30 // it is supposed to handle this check.
|
Chris@0
|
31 if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) {
|
Chris@0
|
32 $query->condition('status', NodeInterface::PUBLISHED);
|
Chris@0
|
33 }
|
Chris@0
|
34 return $query;
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * {@inheritdoc}
|
Chris@0
|
39 */
|
Chris@0
|
40 public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
|
Chris@0
|
41 $node = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
|
Chris@0
|
42
|
Chris@0
|
43 // In order to create a referenceable node, it needs to published.
|
Chris@0
|
44 /** @var \Drupal\node\NodeInterface $node */
|
Chris@17
|
45 $node->setPublished();
|
Chris@0
|
46
|
Chris@0
|
47 return $node;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * {@inheritdoc}
|
Chris@0
|
52 */
|
Chris@0
|
53 public function validateReferenceableNewEntities(array $entities) {
|
Chris@0
|
54 $entities = parent::validateReferenceableNewEntities($entities);
|
Chris@0
|
55 // Mirror the conditions checked in buildEntityQuery().
|
Chris@0
|
56 if (!$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'))) {
|
Chris@0
|
57 $entities = array_filter($entities, function ($node) {
|
Chris@0
|
58 /** @var \Drupal\node\NodeInterface $node */
|
Chris@0
|
59 return $node->isPublished();
|
Chris@0
|
60 });
|
Chris@0
|
61 }
|
Chris@0
|
62 return $entities;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 }
|