Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / src / Form / WidgetsConfig.php
1 <?php
2
3 namespace Drupal\entity_browser\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\entity_browser\WidgetManager;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Widget configuration step in entity browser form wizard.
12  */
13 class WidgetsConfig extends FormBase {
14
15   /**
16    * Entity browser widget plugin manager.
17    *
18    * @var \Drupal\entity_browser\WidgetManager
19    */
20   protected $widgetManager;
21
22   /**
23    * WidgetsConfig constructor.
24    *
25    * @param \Drupal\entity_browser\WidgetManager $widget_manager
26    *   Entity browser widget plugin manager.
27    */
28   function __construct(WidgetManager $widget_manager) {
29     $this->widgetManager = $widget_manager;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('plugin.manager.entity_browser.widget')
38     );
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getFormId() {
45     return 'entity_browser_widgets_config_form';
46   }
47
48   /**
49    * {@inheritdoc}
50    */
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'];
54
55     $widgets = [];
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>';
60     }
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();
66     }
67     $form['widget'] = [
68       '#type' => 'select',
69       '#title' => $this->t('Add widget plugin'),
70       '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets,
71       '#description' => $description,
72       '#ajax' => [
73         'callback' => [get_class($this), 'tableUpdatedAjaxCallback'],
74         'wrapper' => 'widgets',
75         'event' => 'change',
76       ],
77       '#executes_submit_callback' => TRUE,
78       '#submit' => [[get_class($this), 'submitAddWidget']],
79       '#limit_validation_errors' => [['widget']],
80     ];
81     $form_state->unsetValue('widget');
82
83     $form['widgets'] = [
84       '#type' => 'container',
85       '#attributes' => ['id' => 'widgets'],
86     ];
87
88     $form['widgets']['table'] = [
89       '#type' => 'table',
90       '#header' => [
91         $this->t('Form'),
92         $this->t('Operations'),
93         $this->t('Actions'),
94         $this->t('Weight'),
95       ],
96       '#empty' => $this->t('There are no widgets.'),
97       '#tabledrag' => [[
98         'action' => 'order',
99         'relationship' => 'sibling',
100         'group' => 'variant-weight',
101       ],
102       ],
103     ];
104
105     /** @var \Drupal\entity_browser\WidgetInterface $widget */
106     foreach ($entity_browser->getWidgets() as $uuid => $widget) {
107       $row = [
108         '#attributes' => [
109           'class' => ['draggable'],
110         ],
111       ];
112       $row['label'] = [
113         '#type' => 'textfield',
114         '#default_value' => $widget->label(),
115         '#title' => $this->t('Label (%label)', [
116           '%label' => $widget->getPluginDefinition()['label'],
117         ]),
118       ];
119       $row['form'] = [];
120       $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state);
121       $row['remove'] = [
122         '#type' => 'submit',
123         '#value' => $this->t('Delete'),
124         '#name' => 'remove' . $uuid,
125         '#ajax' => [
126           'callback' => [get_class($this), 'tableUpdatedAjaxCallback'],
127           'wrapper' => 'widgets',
128           'event' => 'click',
129         ],
130         '#executes_submit_callback' => TRUE,
131         '#submit' => [[get_class($this), 'submitDeleteWidget']],
132         '#arguments' => $uuid,
133         '#limit_validation_errors' => [],
134       ];
135       $row['weight'] = [
136         '#type' => 'weight',
137         '#default_value' => $widget->getWeight(),
138         '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]),
139         '#title_display' => 'invisible',
140         '#attributes' => [
141           'class' => ['variant-weight'],
142         ],
143       ];
144       $form['widgets']['table'][$uuid] = $row;
145     }
146     return $form;
147   }
148
149   /**
150    * AJAX submit callback for adding widgets to the entity browser.
151    */
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([
160       'id' => $widget,
161       'label' => $widget,
162       'weight' => $weight,
163       // Configuration will be set on the widgets page.
164       'settings' => [],
165     ]);
166     \Drupal::service('user.shared_tempstore')
167       ->get('entity_browser.config')
168       ->set($entity_browser->id(), $cached_values);
169     $form_state->setRebuild();
170   }
171
172   /**
173    * AJAX submit callback for removing widgets from the entity browser.
174    */
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();
184   }
185
186   /**
187    * AJAX callback for all operations that update widgets table.
188    */
189   public static function tableUpdatedAjaxCallback($form, $form_state) {
190     return $form['widgets'];
191   }
192
193   /**
194    * {@inheritdoc}
195    */
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);
202     }
203   }
204
205   /**
206    * {@inheritdoc}
207    */
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']);
217     }
218   }
219
220 }