3 namespace Drupal\Core\Entity\EntityReferenceSelection;
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;
13 * Plugin type manager for Entity Reference Selection plugins.
15 * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
16 * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
19 class SelectionPluginManager extends DefaultPluginManager implements SelectionPluginManagerInterface, FallbackPluginManagerInterface {
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');
28 parent::__construct('Plugin/EntityReferenceSelection', $namespaces, $module_handler, 'Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface', 'Drupal\Core\Entity\Annotation\EntityReferenceSelection');
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.");
39 // Initialize default options.
41 'handler' => $this->getPluginId($options['target_type'], 'default'),
42 'handler_settings' => [],
45 // A specific selection plugin ID was already specified.
46 if (strpos($options['handler'], ':') !== FALSE) {
47 $plugin_id = $options['handler'];
49 // Only a selection group name was specified.
51 $plugin_id = $this->getPluginId($options['target_type'], $options['handler']);
54 return $this->createInstance($plugin_id, $options);
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);
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]);
75 public function getSelectionGroups($entity_type_id) {
77 $definitions = $this->getDefinitions();
79 // Do not display the 'broken' plugin in the UI.
80 unset($definitions['broken']);
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;
94 public function getSelectionHandler(FieldDefinitionInterface $field_definition, EntityInterface $entity = NULL) {
96 'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'),
97 'handler' => $field_definition->getSetting('handler'),
98 'handler_settings' => $field_definition->getSetting('handler_settings') ?: [],
101 return $this->getInstance($options);
107 public function getFallbackPluginId($plugin_id, array $configuration = []) {