comparison core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\taxonomy\Plugin\EntityReferenceSelection;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\taxonomy\Entity\Vocabulary;
9
10 /**
11 * Provides specific access control for the taxonomy_term entity type.
12 *
13 * @EntityReferenceSelection(
14 * id = "default:taxonomy_term",
15 * label = @Translation("Taxonomy Term selection"),
16 * entity_types = {"taxonomy_term"},
17 * group = "default",
18 * weight = 1
19 * )
20 */
21 class TermSelection extends DefaultSelection {
22
23 /**
24 * {@inheritdoc}
25 */
26 public function defaultConfiguration() {
27 return [
28 'sort' => [
29 'field' => 'name',
30 'direction' => 'asc',
31 ]
32 ] + parent::defaultConfiguration();
33 }
34
35 /**
36 * {@inheritdoc}
37 */
38 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
39 $form = parent::buildConfigurationForm($form, $form_state);
40
41 $form['target_bundles']['#title'] = $this->t('Available Vocabularies');
42
43 // Sorting is not possible for taxonomy terms because we use
44 // \Drupal\taxonomy\TermStorageInterface::loadTree() to retrieve matches.
45 $form['sort']['#access'] = FALSE;
46
47 return $form;
48
49 }
50
51 /**
52 * {@inheritdoc}
53 */
54 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
55 if ($match || $limit) {
56 return parent::getReferenceableEntities($match, $match_operator, $limit);
57 }
58
59 $options = [];
60
61 $bundles = $this->entityManager->getBundleInfo('taxonomy_term');
62 $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);
63
64 foreach ($bundle_names as $bundle) {
65 if ($vocabulary = Vocabulary::load($bundle)) {
66 if ($terms = $this->entityManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
67 foreach ($terms as $term) {
68 $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityManager->getTranslationFromContext($term)->label());
69 }
70 }
71 }
72 }
73
74 return $options;
75 }
76
77 }