53b43fdc68926f8d98b96fe8f3e0040f569e08d8
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityReferenceSelection / SelectionPluginManager.php
1 <?php
2
3 namespace Drupal\Core\Entity\EntityReferenceSelection;
4
5 use Drupal\Component\Plugin\FallbackPluginManagerInterface;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Plugin\DefaultPluginManager;
11
12 /**
13  * Plugin type manager for Entity Reference Selection plugins.
14  *
15  * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
16  * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
17  * @see plugin_api
18  */
19 class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
25     $this->alterInfo('entity_reference_selection');
26     $this->setCacheBackend($cache_backend, 'entity_reference_selection_plugins');
27
28     parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface', 'Drupal\Core\Entity\Annotation\EntityReferenceSelection');
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getInstance(array $options) {
35     if (!isset($options['target_type'])) {
36       throw new \InvalidArgumentException("Missing required 'target_type' property for a EntityReferenceSelection plugin.");
37     }
38
39     // Initialize default options.
40     $options += [
41       'handler' => $this->getPluginId($options['target_type'], 'default'),
42       'handler_settings' => [],
43     ];
44
45     // A specific selection plugin ID was already specified.
46     if (strpos($options['handler'], ':') !== FALSE) {
47       $plugin_id = $options['handler'];
48     }
49     // Only a selection group name was specified.
50     else {
51       $plugin_id = $this->getPluginId($options['target_type'], $options['handler']);
52     }
53
54     return $this->createInstance($plugin_id, $options);
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function getPluginId($target_type, $base_plugin_id) {
61     // Get all available selection plugins for this entity type.
62     $selection_handler_groups = $this->getSelectionGroups($target_type);
63
64     // Sort the selection plugins by weight and select the best match.
65     uasort($selection_handler_groups[$base_plugin_id], ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
66     end($selection_handler_groups[$base_plugin_id]);
67     $plugin_id = key($selection_handler_groups[$base_plugin_id]);
68
69     return $plugin_id;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function getSelectionGroups($entity_type_id) {
76     $plugins = [];
77     $definitions = $this->getDefinitions();
78
79     // Do not display the 'broken' plugin in the UI.
80     unset($definitions['broken']);
81
82     foreach ($definitions as $plugin_id => $plugin) {
83       if (empty($plugin['entity_types']) || in_array($entity_type_id, $plugin['entity_types'])) {
84         $plugins[$plugin['group']][$plugin_id] = $plugin;
85       }
86     }
87
88     return $plugins;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
95     $options = [
96       'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
97       'handler' => $field_definition->getSetting('handler'),
98       'handler_settings' => $field_definition->getSetting('handler_settings') ?: [],
99       'entity' => $entity,
100     ];
101     return $this->getInstance($options);
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getFallbackPluginId($plugin_id, array $configuration = []) {
108     return 'broken';
109   }
110
111 }