View source
<?php
namespace Drupal\views\Form;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\views\Render\ViewsRenderPipelineMarkup;
use Drupal\views\ViewExecutable;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class ViewsFormMainForm implements FormInterface, TrustedCallbackInterface {
use StringTranslationTrait;
public function getFormId() {
}
public static function preRenderViewsForm(array $element) {
$search = [];
$replace = [];
foreach ($element['#substitutions']['#value'] as $substitution) {
$field_name = $substitution['field_name'];
$row_id = $substitution['row_id'];
$search[] = $substitution['placeholder'];
$replace[] = isset($element[$field_name][$row_id]) ? \Drupal::service('renderer')
->render($element[$field_name][$row_id]) : '';
}
$substitutions = \Drupal::moduleHandler()
->invokeAll('views_form_substitutions');
foreach ($substitutions as $placeholder => $substitution) {
$search[] = Html::escape($placeholder);
if (!$substitution instanceof MarkupInterface) {
$substitution = Html::escape($substitution);
}
$replace[] = $substitution;
}
$output = str_replace($search, $replace, \Drupal::service('renderer')
->render($element['output']));
$element['output'] = [
'#markup' => ViewsRenderPipelineMarkup::create($output),
];
return $element;
}
public static function trustedCallbacks() {
return [
'preRenderViewsForm',
];
}
public function buildForm(array $form, FormStateInterface $form_state, ViewExecutable $view = NULL, $output = []) {
$form['#prefix'] = '<div class="views-form">';
$form['#suffix'] = '</div>';
$form['#pre_render'][] = [
static::class,
'preRenderViewsForm',
];
$form['output'] = $output;
$form['output']['#weight'] = 50;
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
$substitutions = [];
foreach ($view->field as $field_name => $field) {
$form_element_name = $field_name;
if (method_exists($field, 'form_element_name')) {
$form_element_name = $field
->form_element_name();
}
$method_form_element_row_id_exists = FALSE;
if (method_exists($field, 'form_element_row_id')) {
$method_form_element_row_id_exists = TRUE;
}
$has_form = FALSE;
if (method_exists($field, 'viewsForm')) {
$field
->viewsForm($form, $form_state);
$has_form = TRUE;
}
if ($has_form) {
foreach ($view->result as $row_id => $row) {
if ($method_form_element_row_id_exists) {
$form_element_row_id = $field
->form_element_row_id($row_id);
}
else {
$form_element_row_id = $row_id;
}
$substitutions[] = [
'placeholder' => '<!--form-item-' . $form_element_name . '--' . $form_element_row_id . '-->',
'field_name' => $form_element_name,
'row_id' => $form_element_row_id,
];
}
}
}
$area_handlers = array_merge(array_values($view->header), array_values($view->footer));
$empty = empty($view->result);
foreach ($area_handlers as $area) {
if (method_exists($area, 'viewsForm') && !$area
->viewsFormEmpty($empty)) {
$area
->viewsForm($form, $form_state);
}
}
$form['#substitutions'] = [
'#type' => 'value',
'#value' => $substitutions,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$view = $form_state
->getBuildInfo()['args'][0];
foreach ($view->field as $field) {
if (method_exists($field, 'viewsFormValidate')) {
$field
->viewsFormValidate($form, $form_state);
}
}
foreach ([
'header',
'footer',
] as $area) {
foreach ($view->{$area} as $area_handler) {
if (method_exists($area_handler, 'viewsFormValidate')) {
$area_handler
->viewsFormValidate($form, $form_state);
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$view = $form_state
->getBuildInfo()['args'][0];
foreach ($view->field as $field) {
if (method_exists($field, 'viewsFormSubmit')) {
$field
->viewsFormSubmit($form, $form_state);
}
}
foreach ([
'header',
'footer',
] as $area) {
foreach ($view->{$area} as $area_handler) {
if (method_exists($area_handler, 'viewsFormSubmit')) {
$area_handler
->viewsFormSubmit($form, $form_state);
}
}
}
}
}