3

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?

  • I think you might be looking for this answer here, where they are overriding a core service: drupal.stackexchange.com/a/220759/57 – Kevin Feb 20 '18 at 19:32
  • Thank you Kevin, the linked question helped me a lot, especially the accepted answer there. If I could I would give the bounty to your comment :D – LarS Feb 23 '18 at 13:29
3

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:

<?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->entityManager->getBundleInfo('taxonomy_term');
    $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);

    foreach ($bundle_names as $bundle) {
      if ($vocabulary = Vocabulary::load($bundle)) {
        if ($terms = $this->entityManager->getStorage('taxonomy_term')->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
          foreach ($terms as $term) {
            $options[$vocabulary->id()][$term->id()] = Html::escape($this->entityManager->getTranslationFromContext($term)->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).

  • I am following your way and annotation to define the plugin. But still I can not get the option for reference method. Is there any suggestion from you to save me from this? – David Feb 23 at 10:30
  • Is it part of your "custom" module, enabled and caches rebuilt? – LarS Feb 24 at 15:06
  • Yes, enabled and caches rebuilt. I suppose to be able to see my custom selection as a option when I configure the reference method for taxonomy-term-reference field. Right? But I can not. – David Feb 24 at 19:16
  • However, If I manually change the field.field.page.field_pdct_cate.yml file. And do drush cim -y. Then my custom selection plugin is put into effect. It just does not reflect and shows up as option in the UI. Weird. – David Feb 24 at 19:29
2

You need to create new custom plugin which extends class of target Core plugin.

File: modules/custom/src/Plugin/EntityReferenceSelection/CustomSelection.php

After this you can navigate to field settings and select your method.

  • I already created my custom plugin as code in question shows. I had problems to code the correct annotation, that's why it didn't work. With the help of the comment from Kevin I was able to solve that. But anyway, thank you for taking the time to help me. – LarS Feb 23 '18 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.