View source
<?php
namespace Drupal\entity_browser;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_browser\Events\EntitySelectionEvent;
use Drupal\entity_browser\Events\Events;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Validator\ConstraintViolationList;
abstract class WidgetBase extends PluginBase implements WidgetInterface, ContainerFactoryPluginInterface {
use PluginConfigurationFormTrait;
protected $id;
protected $uuid;
protected $label;
protected $weight;
protected $eventDispatcher;
protected $entityTypeManager;
protected $validationManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->eventDispatcher = $event_dispatcher;
$this->entityTypeManager = $entity_type_manager;
$this->validationManager = $validation_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'), $container
->get('plugin.manager.entity_browser.widget_validation'));
}
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = [];
if ($form_state
->has([
'entity_browser',
'widget_context',
])) {
$this
->handleWidgetContext($form_state
->get([
'entity_browser',
'widget_context',
]));
}
$autoSelect = FALSE;
if ($this
->getPluginDefinition()['auto_select']) {
$autoSelect = $this->configuration['auto_select'];
$form['#attached']['drupalSettings']['entity_browser_widget']['auto_select'] = $autoSelect;
}
if (!$autoSelect) {
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this->configuration['submit_text'],
'#eb_widget_main_submit' => TRUE,
'#attributes' => [
'class' => [
'is-entity-browser-submit',
],
],
'#button_type' => 'primary',
],
];
}
return $form;
}
public function defaultConfiguration() {
$defaultConfig = [
'submit_text' => $this
->t('Select entities'),
];
if ($this
->getPluginDefinition()['auto_select']) {
$defaultConfig['auto_select'] = FALSE;
}
return $defaultConfig;
}
public function getConfiguration() {
return [
'settings' => array_diff_key($this->configuration, [
'entity_browser_id' => 0,
]),
'uuid' => $this
->uuid(),
'weight' => $this
->getWeight(),
'label' => $this
->label(),
'id' => $this
->id(),
];
}
public function setConfiguration(array $configuration) {
$configuration += [
'settings' => [],
'uuid' => '',
'weight' => '',
'label' => '',
'id' => '',
];
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration['settings']);
$this->label = $configuration['label'];
$this->weight = $configuration['weight'];
$this->uuid = $configuration['uuid'];
$this->id = $configuration['id'];
}
public function calculateDependencies() {
return [];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['submit_text'] = [
'#type' => 'textfield',
'#title' => $this
->t('Submit button text'),
'#default_value' => $this->configuration['submit_text'],
];
if ($this
->getPluginDefinition()['auto_select']) {
$form['auto_select'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Automatically submit selection'),
'#default_value' => $this->configuration['auto_select'],
];
}
return $form;
}
public function id() {
return $this->id;
}
public function uuid() {
return $this->uuid;
}
public function label() {
return $this->label;
}
public function setLabel($label) {
$this->label = $label;
return $this;
}
public function getWeight() {
return $this->weight;
}
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
protected abstract function prepareEntities(array $form, FormStateInterface $form_state);
public function validate(array &$form, FormStateInterface $form_state) {
$entities = $this
->prepareEntities($form, $form_state);
$validators = $form_state
->get([
'entity_browser',
'validators',
]);
if ($validators) {
$violations = $this
->runWidgetValidators($entities, $validators);
foreach ($violations as $violation) {
$form_state
->setError($form['widget'], $violation
->getMessage());
}
}
}
protected function runWidgetValidators(array $entities, $validators = []) {
$violations = new ConstraintViolationList();
foreach ($validators as $validator_id => $options) {
$widget_validator = $this->validationManager
->createInstance($validator_id, []);
if ($widget_validator) {
$violations
->addAll($widget_validator
->validate($entities, $options));
}
}
return $violations;
}
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
}
protected function selectEntities(array $entities, FormStateInterface $form_state) {
$selected_entities =& $form_state
->get([
'entity_browser',
'selected_entities',
]);
$selected_entities = array_merge($selected_entities, $entities);
$this->eventDispatcher
->dispatch(Events::SELECTED, new EntitySelectionEvent($this->configuration['entity_browser_id'], $form_state
->get([
'entity_browser',
'instance_uuid',
]), $entities));
}
public function requiresJsCommands() {
return $this
->getPluginDefinition()['auto_select'] && $this
->getConfiguration()['settings']['auto_select'];
}
protected function handleWidgetContext($widget_context) {
foreach ($this
->defaultConfiguration() as $key => $value) {
if (isset($widget_context[$key]) && isset($this->configuration[$key])) {
$this->configuration[$key] = $widget_context[$key];
}
}
}
public function access() {
return AccessResult::allowed();
}
}