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