3 namespace Drupal\entity_browser_entity_form\Plugin\EntityBrowser\Widget;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\ReplaceCommand;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\entity_browser\WidgetBase;
13 use Drupal\entity_browser\WidgetValidationManager;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
19 * Uses a view to provide entity listing in a browser's widget.
21 * @EntityBrowserWidget(
23 * label = @Translation("Entity form"),
24 * description = @Translation("Provides entity form widget."),
28 class EntityForm extends WidgetBase {
31 * The entity type bundle info service.
33 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
35 protected $entityTypeBundleInfo;
38 * The entity display repository.
40 * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
42 protected $entityDisplayRepository;
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 type manager service.
57 * @param \Drupal\entity_browser\WidgetValidationManager $validation_manager
58 * The Widget Validation Manager service.
59 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
60 * The entity type bundle info service.
61 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
62 * The entity display repository.
64 public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $event_dispatcher, EntityTypeManagerInterface $entity_type_manager, WidgetValidationManager $validation_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityDisplayRepositoryInterface $entity_display_repository) {
65 parent::__construct($configuration, $plugin_id, $plugin_definition, $event_dispatcher, $entity_type_manager, $validation_manager);
66 $this->entityTypeBundleInfo = $entity_type_bundle_info;
67 $this->entityDisplayRepository = $entity_display_repository;
73 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
78 $container->get('event_dispatcher'),
79 $container->get('entity_type.manager'),
80 $container->get('plugin.manager.entity_browser.widget_validation'),
81 $container->get('entity_type.bundle.info'),
82 $container->get('entity_display.repository')
89 public function defaultConfiguration() {
91 'entity_type' => NULL,
93 'form_mode' => 'default',
94 'submit_text' => $this->t('Save entity'),
95 ] + parent::defaultConfiguration();
101 public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) {
102 if (empty($this->configuration['entity_type']) || empty($this->configuration['bundle']) || empty($this->configuration['form_mode'])) {
103 return ['#markup' => $this->t('The settings for this widget (Entity type, Bundle or Form mode) are not configured correctly.')];
106 $form = parent::getForm($original_form, $form_state, $additional_widget_parameters);
108 // Pretend to be IEFs submit button.
109 $form['#submit'] = [['Drupal\inline_entity_form\ElementSubmit', 'trigger']];
110 $form['actions']['submit']['#ief_submit_trigger'] = TRUE;
111 $form['actions']['submit']['#ief_submit_trigger_all'] = TRUE;
113 $form['inline_entity_form'] = [
114 '#type' => 'inline_entity_form',
116 '#entity_type' => $this->configuration['entity_type'],
117 '#bundle' => $this->configuration['bundle'],
118 '#form_mode' => $this->configuration['form_mode'],
127 protected function prepareEntities(array $form, FormStateInterface $form_state) {
128 return [$form[$form['#browser_parts']['widget']]['inline_entity_form']['#entity']];
134 public function submit(array &$element, array &$form, FormStateInterface $form_state) {
135 if (!empty($form_state->getTriggeringElement()['#eb_widget_main_submit'])) {
136 $entities = $this->prepareEntities($form, $form_state);
139 function (EntityInterface $entity) {
143 $this->selectEntities($entities, $form_state);
150 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
151 $form = parent::buildConfigurationForm($form, $form_state);
153 $parents = ['table', $this->uuid(), 'form'];
154 $entity_type = $form_state->hasValue(array_merge($parents, ['entity_type'])) ? $form_state->getValue(array_merge($parents, ['entity_type'])) : $this->configuration['entity_type'];
155 $bundle = $form_state->hasValue(array_merge($parents, ['bundle', 'select'])) ? $form_state->getValue(array_merge($parents, ['bundle', 'select'])) : $this->configuration['bundle'];
156 $form_mode = $form_state->hasValue(array_merge($parents, ['form_mode', 'form_select'])) ? $form_state->hasValue(array_merge($parents, ['form_mode', 'form_select'])) : $this->configuration['form_mode'];
158 $definitions = $this->entityTypeManager->getDefinitions();
159 $entity_types = array_combine(
160 array_keys($definitions),
161 array_map(function (EntityTypeInterface $item) {
162 return $item->getLabel();
166 $form['entity_type'] = [
168 '#title' => $this->t('Entity type'),
169 '#options' => $entity_types,
170 '#default_value' => $entity_type,
172 'callback' => [$this, 'updateFormElements'],
178 $definitions = $this->entityTypeBundleInfo->getBundleInfo($entity_type);
179 $bundles = array_map(function ($item) {
180 return $item['label'];
185 '#type' => 'container',
188 '#title' => $this->t('Bundle'),
189 '#options' => $bundles,
190 '#default_value' => $bundle,
192 '#attributes' => ['id' => 'bundle-wrapper-' . $this->uuid()],
195 $form['form_mode'] = [
196 '#type' => 'container',
199 '#title' => $this->t('Form mode'),
200 '#default_value' => $form_mode,
201 '#options' => $this->entityDisplayRepository->getFormModeOptions($entity_type),
203 '#attributes' => ['id' => 'form-mode-wrapper-' . $this->uuid()],
210 * AJAX callback for bundle dropdown update.
212 public function updateBundle($form, FormStateInterface $form_state) {
213 return $form['widgets']['table'][$this->uuid()]['form']['bundle'];
217 * AJAX callback for the Form Mode dropdown update.
219 public function updateFormMode($form, FormStateInterface $form_state) {
220 return $form['widgets']['table'][$this->uuid()]['form']['form_mode'];
224 * AJAX callback to update the two form elements: bundle and form_mode.
226 public function updateFormElements($form, FormStateInterface $form_state) {
227 $response = new AjaxResponse();
228 $response->addCommand(new ReplaceCommand('#bundle-wrapper-' . $this->uuid(), $this->updateBundle($form, $form_state)));
229 $response->addCommand(new ReplaceCommand('#form-mode-wrapper-' . $this->uuid(), $this->updateFormMode($form, $form_state)));
236 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
237 parent::submitConfigurationForm($form, $form_state);
238 $this->configuration['bundle'] = $this->configuration['bundle']['select'];
239 $this->configuration['form_mode'] = $this->configuration['form_mode']['form_select'];