1 | <?php |
||
18 | class WidgetsConfig extends FormBase { |
||
19 | |||
20 | /** |
||
21 | * Entity browser widget plugin manager. |
||
22 | * |
||
23 | * @var \Drupal\entity_browser\WidgetManager |
||
24 | */ |
||
25 | protected $widgetManager; |
||
26 | |||
27 | /** |
||
28 | * WidgetsConfig constructor. |
||
29 | * |
||
30 | * @param \Drupal\entity_browser\WidgetManager $widget_manager |
||
31 | * Entity browser widget plugin manager. |
||
32 | */ |
||
33 | function __construct(WidgetManager $widget_manager) { |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public static function create(ContainerInterface $container) { |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public function getFormId() { |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | public function buildForm(array $form, FormStateInterface $form_state) { |
||
57 | /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
||
58 | $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
||
59 | |||
60 | $widgets = []; |
||
61 | foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) { |
||
62 | $widgets[$plugin_id] = $plugin_definition['label']; |
||
63 | } |
||
64 | $default_widgets = []; |
||
65 | foreach ($entity_browser->getWidgets() as $widget) { |
||
66 | /** @var \Drupal\entity_browser\WidgetInterface $widget */ |
||
67 | $default_widgets[] = $widget->id(); |
||
68 | } |
||
69 | $form['widget'] = [ |
||
70 | '#type' => 'select', |
||
71 | '#title' => $this->t('Add widget plugin'), |
||
72 | '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets, |
||
73 | '#ajax' => [ |
||
74 | 'callback' => [get_class($this), 'tableUpdatedAjaxCallback'], |
||
75 | 'wrapper' => 'widgets', |
||
76 | 'event' => 'change' |
||
77 | ], |
||
78 | '#executes_submit_callback' => TRUE, |
||
79 | '#submit' => [[get_class($this), 'submitAddWidget']], |
||
80 | '#limit_validation_errors' => [['widget']], |
||
81 | ]; |
||
82 | $form_state->unsetValue('widget'); |
||
83 | |||
84 | $form['widgets'] = [ |
||
85 | '#type' => 'container', |
||
86 | '#attributes' => ['id' => 'widgets'], |
||
87 | ]; |
||
88 | |||
89 | $form['widgets']['table'] = [ |
||
90 | '#type' => 'table', |
||
91 | '#header' => [ |
||
92 | $this->t('Form'), |
||
93 | $this->t('Operations'), |
||
94 | $this->t('Actions'), |
||
95 | $this->t('Weight'), |
||
96 | ], |
||
97 | '#empty' => $this->t('There are no widgets.'), |
||
98 | '#tabledrag' => [[ |
||
99 | 'action' => 'order', |
||
100 | 'relationship' => 'sibling', |
||
101 | 'group' => 'variant-weight', |
||
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'), |
||
116 | ]; |
||
117 | $row['form'] = []; |
||
118 | $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state); |
||
119 | $row['remove'] = [ |
||
120 | '#type' => 'submit', |
||
121 | '#value' => $this->t('Delete'), |
||
122 | '#name' => 'remove' . $uuid, |
||
123 | '#ajax' => [ |
||
124 | 'callback' => [get_class($this), 'tableUpdatedAjaxCallback'], |
||
125 | 'wrapper' => 'widgets', |
||
126 | 'event' => 'click' |
||
127 | ], |
||
128 | '#executes_submit_callback' => TRUE, |
||
129 | '#submit' => [[get_class($this), 'submitDeleteWidget']], |
||
130 | '#arguments' => $uuid, |
||
131 | ]; |
||
132 | $row['weight'] = [ |
||
133 | '#type' => 'weight', |
||
134 | '#default_value' => $widget->getWeight(), |
||
135 | '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]), |
||
136 | '#title_display' => 'invisible', |
||
137 | '#attributes' => [ |
||
138 | 'class' => ['variant-weight'], |
||
139 | ], |
||
140 | ]; |
||
141 | $form['widgets']['table'][$uuid] = $row; |
||
142 | } |
||
143 | return $form; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * AJAX submit callback for adding widgets to the entity browser. |
||
148 | */ |
||
149 | public static function submitAddWidget($form, FormStateInterface $form_state) { |
||
168 | |||
169 | /** |
||
170 | * AJAX submit callback for removing widgets from the entity browser. |
||
171 | */ |
||
172 | public static function submitDeleteWidget($form, FormStateInterface $form_state) { |
||
182 | |||
183 | /** |
||
184 | * AJAX callback for all operations that update widgets table. |
||
185 | */ |
||
186 | public static function tableUpdatedAjaxCallback($form, $form_state) { |
||
187 | return $form['widgets']; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function validateForm(array &$form, FormStateInterface $form_state) { |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function submitForm(array &$form, FormStateInterface $form_state) { |
||
221 | |||
222 | } |
||
223 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.