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@0
|
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@0
|
59 $bundles = $this->entityManager->getBundleInfo('taxonomy_term');
|
Chris@0
|
60 $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);
|
Chris@0
|
61
|
Chris@0
|
62 foreach ($bundle_names as $bundle) {
|
Chris@0
|
63 if ($vocabulary = Vocabulary::load($bundle)) {
|
Chris@0
|
64 if ($terms = $this->entityManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
|
Chris@0
|
65 foreach ($terms as $term) {
|
Chris@0
|
66 $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityManager->getTranslationFromContext($term)->label());
|
Chris@0
|
67 }
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 return $options;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 }
|