3 namespace Drupal\Core\Entity\Plugin\Derivative;
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Provides derivative plugins for the DefaultSelection plugin.
13 * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection
14 * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
15 * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
16 * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
19 class DefaultSelectionDeriver extends DeriverBase implements ContainerDeriverInterface {
24 * @var \Drupal\Core\Entity\EntityManagerInterface
26 protected $entityManager;
29 * Creates an SelectionBase object.
31 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
34 public function __construct(EntityManagerInterface $entity_manager) {
35 $this->entityManager = $entity_manager;
41 public static function create(ContainerInterface $container, $base_plugin_id) {
43 $container->get('entity.manager')
50 public function getDerivativeDefinitions($base_plugin_definition) {
51 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
52 $this->derivatives[$entity_type_id] = $base_plugin_definition;
53 $this->derivatives[$entity_type_id]['entity_types'] = [$entity_type_id];
54 $this->derivatives[$entity_type_id]['label'] = t('@entity_type selection', ['@entity_type' => $entity_type->getLabel()]);
55 $this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];
57 // If the entity type doesn't provide a 'label' key in its plugin
58 // definition, we have to use the alternate PhpSelection class as default
59 // plugin, which allows filtering the target entities by their label()
60 // method. The major downside of PhpSelection is that it is more expensive
61 // performance-wise than SelectionBase because it has to load all the
62 // target entities in order to perform the filtering process, regardless
63 // of whether a limit has been passed.
64 // @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\PhpSelection
65 if (!$entity_type->hasKey('label')) {
66 $this->derivatives[$entity_type_id]['class'] = 'Drupal\Core\Entity\Plugin\EntityReferenceSelection\PhpSelection';
70 return parent::getDerivativeDefinitions($base_plugin_definition);