SelectionPluginManager.php 3.6 KB

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