3 namespace Drupal\views_ui\Form\Ajax;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ViewEntityInterface;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Views;
9 use Symfony\Component\HttpFoundation\Request;
12 * Provides a form for configuring an item in the Views UI.
16 class ConfigHandler extends ViewsFormBase {
19 * Constructs a new ConfigHandler object.
21 public function __construct($type = NULL, $id = NULL) {
22 $this->setType($type);
29 public function getFormKey() {
36 public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
37 $this->setType($type);
39 return parent::getForm($view, $display_id, $js);
45 public function getFormId() {
46 return 'views_ui_config_item_form';
52 public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
53 /** @var \Drupal\views\Entity\View $view */
54 $view = $form_state->get('view');
55 $display_id = $form_state->get('display_id');
56 $type = $form_state->get('type');
57 $id = $form_state->get('id');
62 '#theme_wrappers' => ['container'],
63 '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
66 $executable = $view->getExecutable();
67 $save_ui_cache = FALSE;
68 if (!$executable->setDisplay($display_id)) {
69 $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
72 $item = $executable->getHandler($display_id, $type, $id);
75 $handler = $executable->display_handler->getHandler($type, $id);
76 if (empty($handler)) {
77 $form['markup'] = ['#markup' => $this->t("Error: handler for @table > @field doesn't exist!", ['@table' => $item['table'], '@field' => $item['field']])];
80 $types = ViewExecutable::getHandlerTypes();
82 // If this item can come from the default display, show a dropdown
83 // that lets the user choose which display the changes should apply to.
84 if ($executable->display_handler->defaultableSections($types[$type]['plural'])) {
85 $section = $types[$type]['plural'];
86 $form_state->set('section', $section);
87 views_ui_standard_display_dropdown($form, $form_state, $section);
90 // A whole bunch of code to figure out what relationships are valid for
92 $relationships = $executable->display_handler->getOption('relationships');
93 $relationship_options = [];
95 foreach ($relationships as $relationship) {
96 // relationships can't link back to self. But also, due to ordering,
97 // relationships can only link to prior relationships.
98 if ($type == 'relationship' && $id == $relationship['id']) {
101 $relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
102 // ignore invalid/broken relationships.
103 if (empty($relationship_handler)) {
107 // If this relationship is valid for this type, add it to the list.
108 $data = Views::viewsData()->get($relationship['table']);
109 if (isset($data[$relationship['field']]['relationship']['base']) && $base = $data[$relationship['field']]['relationship']['base']) {
110 $base_fields = Views::viewsDataHelper()->fetchFields($base, $type, $executable->display_handler->useGroupBy());
111 if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
112 $relationship_handler->init($executable, $executable->display_handler, $relationship);
113 $relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
118 if (!empty($relationship_options)) {
119 // Make sure the existing relationship is even valid. If not, force
121 $base_fields = Views::viewsDataHelper()->fetchFields($view->get('base_table'), $type, $executable->display_handler->useGroupBy());
122 if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
123 $relationship_options = array_merge(['none' => $this->t('Do not use a relationship')], $relationship_options);
125 $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
126 if (empty($relationship_options[$rel])) {
127 // Pick the first relationship.
128 $rel = key($relationship_options);
129 // We want this relationship option to get saved even if the user
130 // skips submitting the form.
131 $executable->setHandlerOption($display_id, $type, $id, 'relationship', $rel);
132 $save_ui_cache = TRUE;
133 // Re-initialize with new relationship.
134 $item['relationship'] = $rel;
135 $handler->init($executable, $executable->display_handler, $item);
138 $form['options']['relationship'] = [
140 '#title' => $this->t('Relationship'),
141 '#options' => $relationship_options,
142 '#default_value' => $rel,
147 $form['options']['relationship'] = [
153 $form['#title'] = $this->t('Configure @type: @item', ['@type' => $types[$type]['lstitle'], '@item' => $handler->adminLabel()]);
155 if (!empty($handler->definition['help'])) {
156 $form['options']['form_description'] = [
157 '#markup' => $handler->definition['help'],
158 '#theme_wrappers' => ['container'],
159 '#attributes' => ['class' => ['js-form-item form-item description']],
164 $form['#section'] = $display_id . '-' . $type . '-' . $id;
166 // Get form from the handler.
167 $handler->buildOptionsForm($form['options'], $form_state);
168 $form_state->set('handler', $handler);
171 $name = $form_state->get('update_name');
173 $view->getStandardButtons($form, $form_state, 'views_ui_config_item_form', $name);
174 // Add a 'remove' button.
175 $form['actions']['remove'] = [
177 '#value' => $this->t('Remove'),
178 '#submit' => [[$this, 'remove']],
179 '#limit_validation_errors' => [['override']],
180 '#button_type' => 'danger',
184 if ($save_ui_cache) {
194 public function validateForm(array &$form, FormStateInterface $form_state) {
195 $form_state->get('handler')->validateOptionsForm($form['options'], $form_state);
197 if ($form_state->getErrors()) {
198 $form_state->set('rerender', TRUE);
205 public function submitForm(array &$form, FormStateInterface $form_state) {
206 $view = $form_state->get('view');
207 $display_id = $form_state->get('display_id');
208 $id = $form_state->get('id');
209 $handler = $form_state->get('handler');
211 // Run it through the handler's submit function.
212 $handler->submitOptionsForm($form['options'], $form_state);
213 $item = $handler->options;
214 $types = ViewExecutable::getHandlerTypes();
216 // For footer/header $handler_type is area but $type is footer/header.
217 // For all other handle types it's the same.
218 $handler_type = $type = $form_state->get('type');
219 if (!empty($types[$type]['type'])) {
220 $handler_type = $types[$type]['type'];
224 $executable = $view->getExecutable();
225 if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
226 if (empty($executable->query)) {
227 $executable->initQuery();
229 $aggregate = $executable->query->getAggregationInfo();
230 if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
231 $override = $aggregate[$item['group_type']]['handler'][$type];
235 // Create a new handler and unpack the options from the form onto it. We
236 // can use that for storage.
237 $handler = Views::handlerManager($handler_type)->getHandler($item, $override);
238 $handler->init($executable, $executable->display_handler, $item);
240 // Add the incoming options to existing options because items using
241 // the extra form may not have everything in the form here.
242 $options = $handler->submitFormCalculateOptions($handler->options, $form_state->getValue('options', []));
244 // This unpacks only options that are in the definition, ensuring random
245 // extra stuff on the form is not sent through.
246 $handler->unpackOptions($handler->options, $options, NULL, FALSE);
248 // Store the item back on the view
249 $executable->setHandler($display_id, $type, $id, $handler->options);
251 // Ensure any temporary options are removed.
252 if (isset($view->temporary_options[$type][$id])) {
253 unset($view->temporary_options[$type][$id]);
261 * Submit handler for removing an item from a view
263 public function remove(&$form, FormStateInterface $form_state) {
264 $view = $form_state->get('view');
265 $display_id = $form_state->get('display_id');
266 $type = $form_state->get('type');
267 $id = $form_state->get('id');
268 // Store the item back on the view
269 list($was_defaulted, $is_defaulted) = $view->getOverrideValues($form, $form_state);
270 $executable = $view->getExecutable();
271 // If the display selection was changed toggle the override value.
272 if ($was_defaulted != $is_defaulted) {
273 $display = &$executable->displayHandlers->get($display_id);
274 $display->optionsOverride($form, $form_state);
276 $executable->removeHandler($display_id, $type, $id);