View source
<?php
namespace Drupal\entity_browser;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigException;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_browser\Events\Events;
use Drupal\entity_browser\Events\SelectionDoneEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
abstract class SelectionDisplayBase extends PluginBase implements SelectionDisplayInterface, ContainerFactoryPluginInterface {
use PluginConfigurationFormTrait;
protected $label;
protected $eventDispatcher;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->eventDispatcher = $event_dispatcher;
$this->entityTypeManager = $entity_type_manager;
$this
->setConfiguration($configuration);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('event_dispatcher'), $container
->get('entity_type.manager'));
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return array_diff_key($this->configuration, [
'entity_browser_id' => 0,
]);
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function calculateDependencies() {
return [];
}
public function label() {
$this->label;
}
public function validate(array &$form, FormStateInterface $form_state) {
}
public function submit(array &$form, FormStateInterface $form_state) {
}
public function checkPreselectionSupport() {
@trigger_error('checkPreselectionSupport method is deprecated. Use supportsPreselection instead.', E_USER_DEPRECATED);
if (!$this
->getPluginDefinition()['acceptPreselection']) {
throw new ConfigException('Used entity browser selection display does not support preselection.');
}
}
public function supportsPreselection() {
return $this
->getPluginDefinition()['acceptPreselection'];
}
public function supportsJsCommands() {
return $this
->getPluginDefinition()['js_commands'];
}
protected function selectionDone(FormStateInterface $form_state) {
$form_state
->set([
'entity_browser',
'selection_completed',
], TRUE);
$this->eventDispatcher
->dispatch(Events::DONE, new SelectionDoneEvent($this->configuration['entity_browser_id'], $form_state
->get([
'entity_browser',
'instance_uuid',
])));
}
}