3 namespace Drupal\taxonomy\Plugin\EntityReferenceSelection;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Database\Query\SelectInterface;
7 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\taxonomy\Entity\Vocabulary;
12 * Provides specific access control for the taxonomy_term entity type.
14 * @EntityReferenceSelection(
15 * id = "default:taxonomy_term",
16 * label = @Translation("Taxonomy Term selection"),
17 * entity_types = {"taxonomy_term"},
22 class TermSelection extends DefaultSelection {
27 public function entityQueryAlter(SelectInterface $query) {
28 // @todo: How to set access, as vocabulary is now config?
34 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
35 $form = parent::buildConfigurationForm($form, $form_state);
37 $form['target_bundles']['#title'] = $this->t('Available Vocabularies');
39 // Sorting is not possible for taxonomy terms because we use
40 // \Drupal\taxonomy\TermStorageInterface::loadTree() to retrieve matches.
41 $form['sort']['#access'] = FALSE;
50 public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
51 if ($match || $limit) {
52 $this->configuration['handler_settings']['sort'] = ['field' => 'name', 'direction' => 'asc'];
53 return parent::getReferenceableEntities($match, $match_operator, $limit);
58 $bundles = $this->entityManager->getBundleInfo('taxonomy_term');
59 $handler_settings = $this->configuration['handler_settings'];
60 $bundle_names = !empty($handler_settings['target_bundles']) ? $handler_settings['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());