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