PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/core/modules/views/src/Form/ViewsFormMainForm.php

http://github.com/drupal/drupal
PHP | 199 lines | 129 code | 28 blank | 42 comment | 13 complexity | a85469e252c2ae461d49a9097f3e7ef0 MD5 | raw file
  1<?php
  2
  3namespace Drupal\views\Form;
  4
  5use Drupal\Component\Render\MarkupInterface;
  6use Drupal\Component\Utility\Html;
  7use Drupal\Core\Form\FormInterface;
  8use Drupal\Core\Form\FormStateInterface;
  9use Drupal\Core\Security\TrustedCallbackInterface;
 10use Drupal\views\Render\ViewsRenderPipelineMarkup;
 11use Drupal\views\ViewExecutable;
 12
 13class ViewsFormMainForm implements FormInterface, TrustedCallbackInterface {
 14
 15  /**
 16   * {@inheritdoc}
 17   */
 18  public function getFormId() {
 19  }
 20
 21  /**
 22   * Replaces views substitution placeholders.
 23   *
 24   * @param array $element
 25   *   An associative array containing the properties of the element.
 26   *   Properties used: #substitutions, #children.
 27   *
 28   * @return array
 29   *   The $element with prepared variables ready for #theme 'form'
 30   *   in views_form_views_form.
 31   */
 32  public static function preRenderViewsForm(array $element) {
 33    // Placeholders and their substitutions (usually rendered form elements).
 34    $search = [];
 35    $replace = [];
 36
 37    // Add in substitutions provided by the form.
 38    foreach ($element['#substitutions']['#value'] as $substitution) {
 39      $field_name = $substitution['field_name'];
 40      $row_id = $substitution['row_id'];
 41
 42      $search[] = $substitution['placeholder'];
 43      $replace[] = isset($element[$field_name][$row_id]) ? \Drupal::service('renderer')->render($element[$field_name][$row_id]) : '';
 44    }
 45    // Add in substitutions from hook_views_form_substitutions().
 46    $substitutions = \Drupal::moduleHandler()->invokeAll('views_form_substitutions');
 47    foreach ($substitutions as $placeholder => $substitution) {
 48      $search[] = Html::escape($placeholder);
 49      // Ensure that any replacements made are safe to make.
 50      if (!($substitution instanceof MarkupInterface)) {
 51        $substitution = Html::escape($substitution);
 52      }
 53      $replace[] = $substitution;
 54    }
 55
 56    // Apply substitutions to the rendered output.
 57    $output = str_replace($search, $replace, \Drupal::service('renderer')->render($element['output']));
 58    $element['output'] = ['#markup' => ViewsRenderPipelineMarkup::create($output)];
 59
 60    return $element;
 61  }
 62
 63  /**
 64   * {@inheritdoc}
 65   */
 66  public static function trustedCallbacks() {
 67    return ['preRenderViewsForm'];
 68  }
 69
 70  /**
 71   * {@inheritdoc}
 72   */
 73  public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = []) {
 74    $form['#prefix'] = '<div class="views-form">';
 75    $form['#suffix'] = '</div>';
 76
 77    $form['#pre_render'][] = [static::class, 'preRenderViewsForm'];
 78
 79    // Add the output markup to the form array so that it's included when the form
 80    // array is passed to the theme function.
 81    $form['output'] = $output;
 82    // This way any additional form elements will go before the view
 83    // (below the exposed widgets).
 84    $form['output']['#weight'] = 50;
 85
 86    $form['actions'] = [
 87      '#type' => 'actions',
 88    ];
 89    $form['actions']['submit'] = [
 90      '#type' => 'submit',
 91      '#value' => t('Save'),
 92    ];
 93
 94    $substitutions = [];
 95    foreach ($view->field as $field_name => $field) {
 96      $form_element_name = $field_name;
 97      if (method_exists($field, 'form_element_name')) {
 98        $form_element_name = $field->form_element_name();
 99      }
100      $method_form_element_row_id_exists = FALSE;
101      if (method_exists($field, 'form_element_row_id')) {
102        $method_form_element_row_id_exists = TRUE;
103      }
104
105      // If the field provides a views form, allow it to modify the $form array.
106      $has_form = FALSE;
107      if (property_exists($field, 'views_form_callback')) {
108        $callback = $field->views_form_callback;
109        $callback($view, $field, $form, $form_state);
110        $has_form = TRUE;
111      }
112      elseif (method_exists($field, 'viewsForm')) {
113        $field->viewsForm($form, $form_state);
114        $has_form = TRUE;
115      }
116
117      // Build the substitutions array for use in the theme function.
118      if ($has_form) {
119        foreach ($view->result as $row_id => $row) {
120          if ($method_form_element_row_id_exists) {
121            $form_element_row_id = $field->form_element_row_id($row_id);
122          }
123          else {
124            $form_element_row_id = $row_id;
125          }
126
127          $substitutions[] = [
128            'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
129            'field_name' => $form_element_name,
130            'row_id' => $form_element_row_id,
131          ];
132        }
133      }
134    }
135
136    // Give the area handlers a chance to extend the form.
137    $area_handlers = array_merge(array_values($view->header), array_values($view->footer));
138    $empty = empty($view->result);
139    foreach ($area_handlers as $area) {
140      if (method_exists($area, 'viewsForm') && !$area->viewsFormEmpty($empty)) {
141        $area->viewsForm($form, $form_state);
142      }
143    }
144
145    $form['#substitutions'] = [
146      '#type' => 'value',
147      '#value' => $substitutions,
148    ];
149
150    return $form;
151  }
152
153  /**
154   * {@inheritdoc}
155   */
156  public function validateForm(array &$form, FormStateInterface $form_state) {
157    $view = $form_state->getBuildInfo()['args'][0];
158
159    // Call the validation method on every field handler that has it.
160    foreach ($view->field as $field) {
161      if (method_exists($field, 'viewsFormValidate')) {
162        $field->viewsFormValidate($form, $form_state);
163      }
164    }
165
166    // Call the validate method on every area handler that has it.
167    foreach (['header', 'footer'] as $area) {
168      foreach ($view->{$area} as $area_handler) {
169        if (method_exists($area_handler, 'viewsFormValidate')) {
170          $area_handler->viewsFormValidate($form, $form_state);
171        }
172      }
173    }
174  }
175
176  /**
177   * {@inheritdoc}
178   */
179  public function submitForm(array &$form, FormStateInterface $form_state) {
180    $view = $form_state->getBuildInfo()['args'][0];
181
182    // Call the submit method on every field handler that has it.
183    foreach ($view->field as $field) {
184      if (method_exists($field, 'viewsFormSubmit')) {
185        $field->viewsFormSubmit($form, $form_state);
186      }
187    }
188
189    // Call the submit method on every area handler that has it.
190    foreach (['header', 'footer'] as $area) {
191      foreach ($view->{$area} as $area_handler) {
192        if (method_exists($area_handler, 'viewsFormSubmit')) {
193          $area_handler->viewsFormSubmit($form, $form_state);
194        }
195      }
196    }
197  }
198
199}