DefaultSelectionDeriver.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Drupal\Core\Entity\Plugin\Derivative;
  3. use Drupal\Component\Plugin\Derivative\DeriverBase;
  4. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9. * Provides derivative plugins for the DefaultSelection plugin.
  10. *
  11. * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection
  12. * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager
  13. * @see \Drupal\Core\Entity\Annotation\EntityReferenceSelection
  14. * @see \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
  15. * @see plugin_api
  16. */
  17. class DefaultSelectionDeriver extends DeriverBase implements ContainerDeriverInterface {
  18. use DeprecatedServicePropertyTrait;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  23. /**
  24. * The entity type manager.
  25. *
  26. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  27. */
  28. protected $entityTypeManager;
  29. /**
  30. * Creates a DefaultSelectionDeriver object.
  31. *
  32. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  33. * The entity type manager.
  34. */
  35. public function __construct(EntityTypeManagerInterface $entity_type_manager) {
  36. $this->entityTypeManager = $entity_type_manager;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public static function create(ContainerInterface $container, $base_plugin_id) {
  42. return new static(
  43. $container->get('entity_type.manager')
  44. );
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getDerivativeDefinitions($base_plugin_definition) {
  50. foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
  51. $this->derivatives[$entity_type_id] = $base_plugin_definition;
  52. $this->derivatives[$entity_type_id]['entity_types'] = [$entity_type_id];
  53. $this->derivatives[$entity_type_id]['label'] = t('@entity_type selection', ['@entity_type' => $entity_type->getLabel()]);
  54. $this->derivatives[$entity_type_id]['base_plugin_label'] = (string) $base_plugin_definition['label'];
  55. // If the entity type doesn't provide a 'label' key in its plugin
  56. // definition, we have to use the alternate PhpSelection class as default
  57. // plugin, which allows filtering the target entities by their label()
  58. // method. The major downside of PhpSelection is that it is more expensive
  59. // performance-wise than DefaultSelection because it has to load all the
  60. // target entities in order to perform the filtering process, regardless
  61. // of whether a limit has been passed.
  62. // @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\PhpSelection
  63. if (!$entity_type->hasKey('label')) {
  64. $this->derivatives[$entity_type_id]['class'] = 'Drupal\Core\Entity\Plugin\EntityReferenceSelection\PhpSelection';
  65. }
  66. }
  67. return parent::getDerivativeDefinitions($base_plugin_definition);
  68. }
  69. }