SelectionTrait.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Drupal\Core\Entity\EntityReferenceSelection;
  3. use Drupal\Core\Entity\EntityManagerInterface;
  4. use Drupal\Core\Extension\ModuleHandlerInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. /**
  8. * Provides common methods and injects services for core selection handlers.
  9. */
  10. trait SelectionTrait {
  11. /**
  12. * The entity manager service.
  13. *
  14. * @var \Drupal\Core\Entity\EntityManagerInterface
  15. */
  16. protected $entityManager;
  17. /**
  18. * The module handler service.
  19. *
  20. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  21. */
  22. protected $moduleHandler;
  23. /**
  24. * The current user.
  25. *
  26. * @var \Drupal\Core\Session\AccountInterface
  27. */
  28. protected $currentUser;
  29. /**
  30. * Constructs a new selection object.
  31. *
  32. * @param array $configuration
  33. * A configuration array containing information about the plugin instance.
  34. * @param string $plugin_id
  35. * The plugin_id for the plugin instance.
  36. * @param mixed $plugin_definition
  37. * The plugin implementation definition.
  38. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  39. * The entity manager service.
  40. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  41. * The module handler service.
  42. * @param \Drupal\Core\Session\AccountInterface $current_user
  43. * The current user.
  44. */
  45. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user) {
  46. parent::__construct($configuration, $plugin_id, $plugin_definition);
  47. $this->entityManager = $entity_manager;
  48. $this->moduleHandler = $module_handler;
  49. $this->currentUser = $current_user;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  55. return new static(
  56. $configuration,
  57. $plugin_id,
  58. $plugin_definition,
  59. $container->get('entity.manager'),
  60. $container->get('module_handler'),
  61. $container->get('current_user')
  62. );
  63. }
  64. }