2 added 126 characters in body
source | link

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).

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"). 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).

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).

1
source | link

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"). 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).