Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\taxonomy\Plugin\EntityReferenceSelection;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Html;
|
Chris@0
|
6 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Provides specific access control for the taxonomy_term entity type.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @EntityReferenceSelection(
|
Chris@0
|
14 * id = "default:taxonomy_term",
|
Chris@0
|
15 * label = @Translation("Taxonomy Term selection"),
|
Chris@0
|
16 * entity_types = {"taxonomy_term"},
|
Chris@0
|
17 * group = "default",
|
Chris@0
|
18 * weight = 1
|
Chris@0
|
19 * )
|
Chris@0
|
20 */
|
Chris@0
|
21 class TermSelection extends DefaultSelection {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * {@inheritdoc}
|
Chris@0
|
25 */
|
Chris@0
|
26 public function defaultConfiguration() {
|
Chris@0
|
27 return [
|
Chris@0
|
28 'sort' => [
|
Chris@0
|
29 'field' => 'name',
|
Chris@0
|
30 'direction' => 'asc',
|
Chris@17
|
31 ],
|
Chris@0
|
32 ] + parent::defaultConfiguration();
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
39 $form = parent::buildConfigurationForm($form, $form_state);
|
Chris@0
|
40
|
Chris@0
|
41 // Sorting is not possible for taxonomy terms because we use
|
Chris@0
|
42 // \Drupal\taxonomy\TermStorageInterface::loadTree() to retrieve matches.
|
Chris@0
|
43 $form['sort']['#access'] = FALSE;
|
Chris@0
|
44
|
Chris@0
|
45 return $form;
|
Chris@0
|
46
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@0
|
52 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
|
Chris@0
|
53 if ($match || $limit) {
|
Chris@0
|
54 return parent::getReferenceableEntities($match, $match_operator, $limit);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 $options = [];
|
Chris@0
|
58
|
Chris@18
|
59 $bundles = $this->entityTypeBundleInfo->getBundleInfo('taxonomy_term');
|
Chris@0
|
60 $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);
|
Chris@0
|
61
|
Chris@17
|
62 $has_admin_access = $this->currentUser->hasPermission('administer taxonomy');
|
Chris@17
|
63 $unpublished_terms = [];
|
Chris@0
|
64 foreach ($bundle_names as $bundle) {
|
Chris@0
|
65 if ($vocabulary = Vocabulary::load($bundle)) {
|
Chris@17
|
66 /** @var \Drupal\taxonomy\TermInterface[] $terms */
|
Chris@18
|
67 if ($terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
|
Chris@0
|
68 foreach ($terms as $term) {
|
Chris@17
|
69 if (!$has_admin_access && (!$term->isPublished() || in_array($term->parent->target_id, $unpublished_terms))) {
|
Chris@17
|
70 $unpublished_terms[] = $term->id();
|
Chris@17
|
71 continue;
|
Chris@17
|
72 }
|
Chris@18
|
73 $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityRepository->getTranslationFromContext($term)->label());
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return $options;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@17
|
82 /**
|
Chris@17
|
83 * {@inheritdoc}
|
Chris@17
|
84 */
|
Chris@17
|
85 public function countReferenceableEntities($match = NULL, $match_operator = 'CONTAINS') {
|
Chris@17
|
86 if ($match) {
|
Chris@17
|
87 return parent::countReferenceableEntities($match, $match_operator);
|
Chris@17
|
88 }
|
Chris@17
|
89
|
Chris@17
|
90 $total = 0;
|
Chris@17
|
91 $referenceable_entities = $this->getReferenceableEntities($match, $match_operator, 0);
|
Chris@17
|
92 foreach ($referenceable_entities as $bundle => $entities) {
|
Chris@17
|
93 $total += count($entities);
|
Chris@17
|
94 }
|
Chris@17
|
95 return $total;
|
Chris@17
|
96 }
|
Chris@17
|
97
|
Chris@17
|
98 /**
|
Chris@17
|
99 * {@inheritdoc}
|
Chris@17
|
100 */
|
Chris@17
|
101 protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
|
Chris@17
|
102 $query = parent::buildEntityQuery($match, $match_operator);
|
Chris@17
|
103
|
Chris@17
|
104 // Adding the 'taxonomy_term_access' tag is sadly insufficient for terms:
|
Chris@17
|
105 // core requires us to also know about the concept of 'published' and
|
Chris@17
|
106 // 'unpublished'.
|
Chris@17
|
107 if (!$this->currentUser->hasPermission('administer taxonomy')) {
|
Chris@17
|
108 $query->condition('status', 1);
|
Chris@17
|
109 }
|
Chris@17
|
110 return $query;
|
Chris@17
|
111 }
|
Chris@17
|
112
|
Chris@17
|
113 /**
|
Chris@17
|
114 * {@inheritdoc}
|
Chris@17
|
115 */
|
Chris@17
|
116 public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
|
Chris@17
|
117 $term = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
|
Chris@17
|
118
|
Chris@17
|
119 // In order to create a referenceable term, it needs to published.
|
Chris@17
|
120 /** @var \Drupal\taxonomy\TermInterface $term */
|
Chris@17
|
121 $term->setPublished();
|
Chris@17
|
122
|
Chris@17
|
123 return $term;
|
Chris@17
|
124 }
|
Chris@17
|
125
|
Chris@17
|
126 /**
|
Chris@17
|
127 * {@inheritdoc}
|
Chris@17
|
128 */
|
Chris@17
|
129 public function validateReferenceableNewEntities(array $entities) {
|
Chris@17
|
130 $entities = parent::validateReferenceableNewEntities($entities);
|
Chris@17
|
131 // Mirror the conditions checked in buildEntityQuery().
|
Chris@17
|
132 if (!$this->currentUser->hasPermission('administer taxonomy')) {
|
Chris@17
|
133 $entities = array_filter($entities, function ($term) {
|
Chris@17
|
134 /** @var \Drupal\taxonomy\TermInterface $term */
|
Chris@17
|
135 return $term->isPublished();
|
Chris@17
|
136 });
|
Chris@17
|
137 }
|
Chris@17
|
138 return $entities;
|
Chris@17
|
139 }
|
Chris@17
|
140
|
Chris@0
|
141 }
|