How to correctly modify Taxonomy Term selection Plugin I want some slight modifications of Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection::getReferenceableEntities(), for example to not use - to indicate term hierarchy. How can that be achieved?

I tried to extend that class in a custom module like that:

/**
 * Provides specific access control for the taxonomy_term entity type.
 *
 * @EntityReferenceSelection(
 *   id = "default:custom_taxonomy_term",
 *   label = @Translation("Custom Taxonomy Term selection"),
 *   entity_types = {"taxonomy_term"},
 *   group = "custom",
 *   weight = 2
 * )
 */
class CustomTermSelection extends TermSelection {

  /**
   * {@inheritdoc}
   */
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    // Customization will be here...
  }
}


The plugin seems to be detected, it appears while debugging $selection_plugins in Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsForm(), but it won't appear as reference method on editing the term reference field.

What else do I need to modify to get it work?
The annotation in the sample code of my question was wrong. With the help of the first comment to this issue I was able to solve the problem (see accepted answer there). Although this question is a bit different it might be marked as duplicate to How to display more than 10 items in link widget autocomplete?.

Anyway I want to leave the custom plugin including the annotation:

&lt;?php

namespace Drupal\custom\Plugin\EntityReferenceSelection;

use Drupal\Component\Utility\Html;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection;

/**
 * Provides specific access control for the taxonomy_term entity type.
 *
 * @EntityReferenceSelection(
 *   id = "custom:taxonomy_term",
 *   label = @Translation("Custom Taxonomy Term selection"),
 *   base_plugin_label = @Translation("Custom Taxonomy Term selection"),
 *   entity_types = {"taxonomy_term"},
 *   group = "custom",
 *   weight = 2
 * )
 */
class CustomTermSelection extends TermSelection {

  /**
   * {@inheritdoc}
   */
  public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    if ($match || $limit) {
      return parent::getReferenceableEntities($match, $match_operator, $limit);
    }

    $options = [];

    $bundles = $this-&gt;entityManager-&gt;getBundleInfo('taxonomy_term');
    $bundle_names = $this-&gt;getConfiguration()['target_bundles'] ?: array_keys($bundles);

    foreach ($bundle_names as $bundle) {
      if ($vocabulary = Vocabulary::load($bundle)) {
        if ($terms = $this-&gt;entityManager-&gt;getStorage('taxonomy_term')-&gt;loadTree($vocabulary-&gt;id(), 0, NULL, TRUE)) {
          foreach ($terms as $term) {
            $options[$vocabulary-&gt;id()][$term-&gt;id()] = Html::escape($this-&gt;entityManager-&gt;getTranslationFromContext($term)-&gt;label());
          }
        }
      }
    }

    return $options;
  }

}


One thing I want to point out, which was not part of the above linked answer is the base_plugin_label = @Translation("Custom Taxonomy Term selection") (solution found on d.o. issue). Without that I got the following notice:


  Notice: Undefined index: base_plugin_label in Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem->fieldSettingsForm() (line 336 of core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php).