3 namespace Drupal\entity_browser;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Config\ConfigException;
7 use Drupal\Core\Plugin\PluginBase;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\entity_browser\Events\Events;
12 use Drupal\entity_browser\Events\SelectionDoneEvent;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17 * Base implementation for selection display plugins.
19 abstract class SelectionDisplayBase extends PluginBase implements SelectionDisplayInterface, ContainerFactoryPluginInterface {
21 use PluginConfigurationFormTrait;
31 * Event dispatcher service.
33 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
35 protected $eventDispatcher;
38 * Entity manager service.
40 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
42 protected $entityTypeManager;
45 * Constructs widget plugin.
47 * @param array $configuration
48 * A configuration array containing information about the plugin instance.
49 * @param string $plugin_id
50 * The plugin_id for the plugin instance.
51 * @param mixed $plugin_definition
52 * The plugin implementation definition.
53 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
54 * Event dispatcher service.
55 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
56 * The entity manager type service.
58 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager) {
59 parent::__construct($configuration, $plugin_id, $plugin_definition);
60 $this->eventDispatcher = $event_dispatcher;
61 $this->entityTypeManager = $entity_type_manager;
62 $this->setConfiguration($configuration);
68 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73 $container->get('event_dispatcher'),
74 $container->get('entity_type.manager')
81 public function defaultConfiguration() {
88 public function getConfiguration() {
89 return array_diff_key(
91 ['entity_browser_id' => 0]
98 public function setConfiguration(array $configuration) {
99 $this->configuration = NestedArray::mergeDeep(
100 $this->defaultConfiguration(),
108 public function calculateDependencies() {
115 public function label() {
122 public function validate(array &$form, FormStateInterface $form_state) {}
127 public function submit(array &$form, FormStateInterface $form_state) {}
132 public function checkPreselectionSupport() {
133 if (!$this->getPluginDefinition()['acceptPreselection']) {
134 throw new ConfigException('Used entity browser selection display does not support preselection.');
141 public function supportsJsCommands() {
142 return $this->getPluginDefinition()['js_commands'];
146 * Marks selection as done - sets value in form state and dispatches event.
148 protected function selectionDone(FormStateInterface $form_state) {
149 $form_state->set(['entity_browser', 'selection_completed'], TRUE);
150 $this->eventDispatcher->dispatch(
152 new SelectionDoneEvent(
153 $this->configuration['entity_browser_id'],
154 $form_state->get(['entity_browser', 'instance_uuid'])