View source
<?php
namespace Drupal\lightning_media\Plugin\EntityBrowser\Widget;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_browser\WidgetBase;
use Drupal\inline_entity_form\ElementSubmit;
use Drupal\lightning_media\Exception\IndeterminateBundleException;
use Drupal\lightning_media\MediaHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EntityFormProxy extends WidgetBase {
protected $helper;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $widget_validation_manager, MediaHelper $helper) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $widget_validation_manager);
$this->helper = $helper;
}
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'), $container
->get('lightning.media_helper'));
}
protected function getAllowedBundles(FormStateInterface $form_state) {
return (array) $form_state
->get([
'entity_browser',
'widget_context',
'target_bundles',
]);
}
public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
$form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
if (isset($form['actions'])) {
$form['actions']['#weight'] = 100;
}
$form['entity'] = [
'#prefix' => '<div id="entity">',
'#suffix' => '</div>',
'#weight' => 99,
];
$value = $this
->getInputValue($form_state);
if (empty($value)) {
$form['entity']['#markup'] = NULL;
return $form;
}
try {
$entity = $this->helper
->createFromInput($value, $this
->getAllowedBundles($form_state));
} catch (IndeterminateBundleException $e) {
return $form;
}
$form['entity'] += [
'#type' => 'inline_entity_form',
'#entity_type' => $entity
->getEntityTypeId(),
'#bundle' => $entity
->bundle(),
'#default_value' => $entity,
'#form_mode' => $this->configuration['form_mode'],
];
ElementSubmit::addCallback($form['actions']['submit'], $form_state
->getCompleteForm());
return $form;
}
protected function prepareEntities(array $form, FormStateInterface $form_state) {
if (isset($form['widget']['entity']['#entity'])) {
return [
$form['widget']['entity']['#entity'],
];
}
else {
return [];
}
}
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
$value = $this
->getInputValue($form_state);
try {
$this->helper
->getBundleFromInput($value, TRUE, $this
->getAllowedBundles($form_state));
} catch (IndeterminateBundleException $e) {
$form_state
->setError($form['widget'], $e
->getMessage());
}
}
public function submit(array &$element, array &$form, FormStateInterface $form_state) {
$entity = $form['widget']['entity']['#entity'];
$this
->selectEntities([
$entity,
], $form_state);
}
public static function ajax(array &$form, FormStateInterface $form_state) {
return (new AjaxResponse())
->addCommand(new ReplaceCommand('#entity', $form['widget']['entity']));
}
protected function getInputValue(FormStateInterface $form_state) {
return $form_state
->getValue('input');
}
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
$configuration['form_mode'] = 'media_browser';
return $configuration;
}
}