Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / views / src / Form / ViewsFormMainForm.php
1 <?php
2
3 namespace Drupal\views\Form;
4
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\ViewExecutable;
8
9 class ViewsFormMainForm implements FormInterface {
10
11   /**
12    * {@inheritdoc}
13    */
14   public function getFormId() {
15   }
16
17   /**
18    * {@inheritdoc}
19    */
20   public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = []) {
21     $form['#prefix'] = '<div class="views-form">';
22     $form['#suffix'] = '</div>';
23
24     $form['#pre_render'][] = 'views_pre_render_views_form_views_form';
25
26     // Add the output markup to the form array so that it's included when the form
27     // array is passed to the theme function.
28     $form['output'] = $output;
29     // This way any additional form elements will go before the view
30     // (below the exposed widgets).
31     $form['output']['#weight'] = 50;
32
33     $form['actions'] = [
34       '#type' => 'actions',
35     ];
36     $form['actions']['submit'] = [
37       '#type' => 'submit',
38       '#value' => t('Save'),
39     ];
40
41     $substitutions = [];
42     foreach ($view->field as $field_name => $field) {
43       $form_element_name = $field_name;
44       if (method_exists($field, 'form_element_name')) {
45         $form_element_name = $field->form_element_name();
46       }
47       $method_form_element_row_id_exists = FALSE;
48       if (method_exists($field, 'form_element_row_id')) {
49         $method_form_element_row_id_exists = TRUE;
50       }
51
52       // If the field provides a views form, allow it to modify the $form array.
53       $has_form = FALSE;
54       if (property_exists($field, 'views_form_callback')) {
55         $callback = $field->views_form_callback;
56         $callback($view, $field, $form, $form_state);
57         $has_form = TRUE;
58       }
59       elseif (method_exists($field, 'viewsForm')) {
60         $field->viewsForm($form, $form_state);
61         $has_form = TRUE;
62       }
63
64       // Build the substitutions array for use in the theme function.
65       if ($has_form) {
66         foreach ($view->result as $row_id => $row) {
67           if ($method_form_element_row_id_exists) {
68             $form_element_row_id = $field->form_element_row_id($row_id);
69           }
70           else {
71             $form_element_row_id = $row_id;
72           }
73
74           $substitutions[] = [
75             'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
76             'field_name' => $form_element_name,
77             'row_id' => $form_element_row_id,
78           ];
79         }
80       }
81     }
82
83     // Give the area handlers a chance to extend the form.
84     $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
85     $empty = empty($view->result);
86     foreach ($area_handlers as $area) {
87       if (method_exists($area, 'viewsForm') && !$area->viewsFormEmpty($empty)) {
88         $area->viewsForm($form, $form_state);
89       }
90     }
91
92     $form['#substitutions'] = [
93       '#type' => 'value',
94       '#value' => $substitutions,
95     ];
96
97     return $form;
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function validateForm(array &$form, FormStateInterface $form_state) {
104     $view = $form_state->getBuildInfo()['args'][0];
105
106     // Call the validation method on every field handler that has it.
107     foreach ($view->field as $field) {
108       if (method_exists($field, 'viewsFormValidate')) {
109         $field->viewsFormValidate($form, $form_state);
110       }
111     }
112
113     // Call the validate method on every area handler that has it.
114     foreach (['header', 'footer'] as $area) {
115       foreach ($view->{$area} as $area_handler) {
116         if (method_exists($area_handler, 'viewsFormValidate')) {
117           $area_handler->viewsFormValidate($form, $form_state);
118         }
119       }
120     }
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function submitForm(array &$form, FormStateInterface $form_state) {
127     $view = $form_state->getBuildInfo()['args'][0];
128
129     // Call the submit method on every field handler that has it.
130     foreach ($view->field as $field) {
131       if (method_exists($field, 'viewsFormSubmit')) {
132         $field->viewsFormSubmit($form, $form_state);
133       }
134     }
135
136     // Call the submit method on every area handler that has it.
137     foreach (['header', 'footer'] as $area) {
138       foreach ($view->{$area} as $area_handler) {
139         if (method_exists($area_handler, 'viewsFormSubmit')) {
140           $area_handler->viewsFormSubmit($form, $form_state);
141         }
142       }
143     }
144   }
145
146 }