3 namespace Drupal\taxonomy\Plugin\EntityReferenceSelection;
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;
11 * Provides specific access control for the taxonomy_term entity type.
13 * @EntityReferenceSelection(
14 * id = "default:taxonomy_term",
15 * label = @Translation("Taxonomy Term selection"),
16 * entity_types = {"taxonomy_term"},
21 class TermSelection extends DefaultSelection {
26 public function defaultConfiguration() {
32 ] + parent::defaultConfiguration();
38 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
39 $form = parent::buildConfigurationForm($form, $form_state);
41 // Sorting is not possible for taxonomy terms because we use
42 // \Drupal\taxonomy\TermStorageInterface::loadTree() to retrieve matches.
43 $form['sort']['#access'] = FALSE;
52 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
53 if ($match || $limit) {
54 return parent::getReferenceableEntities($match, $match_operator, $limit);
59 $bundles = $this->entityManager->getBundleInfo('taxonomy_term');
60 $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);
62 foreach ($bundle_names as $bundle) {
63 if ($vocabulary = Vocabulary::load($bundle)) {
64 if ($terms = $this->entityManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
65 foreach ($terms as $term) {
66 $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityManager->getTranslationFromContext($term)->label());