Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Plugin / Derivative / DefaultSelectionDeriver.php
1 <?php
2
3 namespace Drupal\Core\Entity\Plugin\Derivative;
4
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;
9
10 /**
11  * Provides derivative plugins for the DefaultSelection plugin.
12  *
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
17  * @see plugin_api
18  */
19 class DefaultSelectionDeriver extends DeriverBase implements ContainerDeriverInterface {
20
21   /**
22    * The entity manager
23    *
24    * @var \Drupal\Core\Entity\EntityManagerInterface
25    */
26   protected $entityManager;
27
28   /**
29    * Creates an SelectionBase object.
30    *
31    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
32    *   The entity manager.
33    */
34   public function __construct(EntityManagerInterface $entity_manager) {
35     $this->entityManager = $entity_manager;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container, $base_plugin_id) {
42     return new static(
43       $container->get('entity.manager')
44     );
45   }
46
47   /**
48    * {@inheritdoc}
49    */
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'];
56
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';
67       }
68     }
69
70     return parent::getDerivativeDefinitions($base_plugin_definition);
71   }
72
73 }