3 namespace Drupal\entity_browser\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity_browser\WidgetManager;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Widget configuration step in entity browser form wizard.
13 class WidgetsConfig extends FormBase {
16 * Entity browser widget plugin manager.
18 * @var \Drupal\entity_browser\WidgetManager
20 protected $widgetManager;
23 * WidgetsConfig constructor.
25 * @param \Drupal\entity_browser\WidgetManager $widget_manager
26 * Entity browser widget plugin manager.
28 function __construct(WidgetManager $widget_manager) {
29 $this->widgetManager = $widget_manager;
35 public static function create(ContainerInterface $container) {
37 $container->get('plugin.manager.entity_browser.widget')
44 public function getFormId() {
45 return 'entity_browser_widgets_config_form';
51 public function buildForm(array $form, FormStateInterface $form_state) {
52 /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
53 $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
56 $description = $this->t('The available plugins are:') . '<ul>';
57 foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) {
58 $widgets[$plugin_id] = $plugin_definition['label'];
59 $description .= '<li><b>' . $plugin_definition['label'] . ':</b> ' . $plugin_definition['description'] . '</li>';
61 $description .= '</ul>';
62 $default_widgets = [];
63 foreach ($entity_browser->getWidgets() as $widget) {
64 /** @var \Drupal\entity_browser\WidgetInterface $widget */
65 $default_widgets[] = $widget->id();
69 '#title' => $this->t('Add widget plugin'),
70 '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets,
71 '#description' => $description,
73 'callback' => [get_class($this), 'tableUpdatedAjaxCallback'],
74 'wrapper' => 'widgets',
77 '#executes_submit_callback' => TRUE,
78 '#submit' => [[get_class($this), 'submitAddWidget']],
79 '#limit_validation_errors' => [['widget']],
81 $form_state->unsetValue('widget');
84 '#type' => 'container',
85 '#attributes' => ['id' => 'widgets'],
88 $form['widgets']['table'] = [
92 $this->t('Operations'),
96 '#empty' => $this->t('There are no widgets.'),
99 'relationship' => 'sibling',
100 'group' => 'variant-weight',
105 /** @var \Drupal\entity_browser\WidgetInterface $widget */
106 foreach ($entity_browser->getWidgets() as $uuid => $widget) {
109 'class' => ['draggable'],
113 '#type' => 'textfield',
114 '#default_value' => $widget->label(),
115 '#title' => $this->t('Label (%label)', [
116 '%label' => $widget->getPluginDefinition()['label'],
120 $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state);
123 '#value' => $this->t('Delete'),
124 '#name' => 'remove' . $uuid,
126 'callback' => [get_class($this), 'tableUpdatedAjaxCallback'],
127 'wrapper' => 'widgets',
130 '#executes_submit_callback' => TRUE,
131 '#submit' => [[get_class($this), 'submitDeleteWidget']],
132 '#arguments' => $uuid,
133 '#limit_validation_errors' => [],
137 '#default_value' => $widget->getWeight(),
138 '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]),
139 '#title_display' => 'invisible',
141 'class' => ['variant-weight'],
144 $form['widgets']['table'][$uuid] = $row;
150 * AJAX submit callback for adding widgets to the entity browser.
152 public static function submitAddWidget($form, FormStateInterface $form_state) {
153 $cached_values = $form_state->getTemporaryValue('wizard');
154 /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
155 $entity_browser = $cached_values['entity_browser'];
156 $widgets_num = count($entity_browser->getWidgets());
157 $widget = $form_state->getValue('widget');
158 $weight = $widgets_num + 1;
159 $entity_browser->addWidget([
163 // Configuration will be set on the widgets page.
166 \Drupal::service('user.shared_tempstore')
167 ->get('entity_browser.config')
168 ->set($entity_browser->id(), $cached_values);
169 $form_state->setRebuild();
173 * AJAX submit callback for removing widgets from the entity browser.
175 public static function submitDeleteWidget($form, FormStateInterface $form_state) {
176 $cached_values = $form_state->getTemporaryValue('wizard');
177 /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
178 $entity_browser = $cached_values['entity_browser'];
179 $entity_browser->deleteWidget($entity_browser->getWidget($form_state->getTriggeringElement()['#arguments']));
180 \Drupal::service('user.shared_tempstore')
181 ->get('entity_browser.config')
182 ->set($entity_browser->id(), $cached_values);
183 $form_state->setRebuild();
187 * AJAX callback for all operations that update widgets table.
189 public static function tableUpdatedAjaxCallback($form, $form_state) {
190 return $form['widgets'];
196 public function validateForm(array &$form, FormStateInterface $form_state) {
197 /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
198 $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
199 /** @var \Drupal\entity_browser\WidgetInterface $widget */
200 foreach ($entity_browser->getWidgets() as $widget) {
201 $widget->validateConfigurationForm($form, $form_state);
208 public function submitForm(array &$form, FormStateInterface $form_state) {
209 /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
210 $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
211 $table = $form_state->getValue('table');
212 /** @var \Drupal\entity_browser\WidgetInterface $widget */
213 foreach ($entity_browser->getWidgets() as $uuid => $widget) {
214 $widget->submitConfigurationForm($form, $form_state);
215 $widget->setWeight($table[$uuid]['weight']);
216 $widget->setLabel($table[$uuid]['label']);