function ViewsFormMainForm::buildForm

Same name and namespace in other branches
  1. 9 core/modules/views/src/Form/ViewsFormMainForm.php \Drupal\views\Form\ViewsFormMainForm::buildForm()
  2. 10 core/modules/views/src/Form/ViewsFormMainForm.php \Drupal\views\Form\ViewsFormMainForm::buildForm()
  3. 11.x core/modules/views/src/Form/ViewsFormMainForm.php \Drupal\views\Form\ViewsFormMainForm::buildForm()

Overrides FormInterface::buildForm

File

core/modules/views/src/Form/ViewsFormMainForm.php, line 73

Class

ViewsFormMainForm

Namespace

Drupal\views\Form

Code

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',
    ];
    // Add the output markup to the form array so that it's included when the form
    // array is passed to the theme function.
    $form['output'] = $output;
    // This way any additional form elements will go before the view
    // (below the exposed widgets).
    $form['output']['#weight'] = 50;
    $form['actions'] = [
        '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => 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;
        }
        // If the field provides a views form, allow it to modify the $form array.
        $has_form = FALSE;
        if (property_exists($field, 'views_form_callback')) {
            $callback = $field->views_form_callback;
            $callback($view, $field, $form, $form_state);
            $has_form = TRUE;
        }
        elseif (method_exists($field, 'viewsForm')) {
            $field->viewsForm($form, $form_state);
            $has_form = TRUE;
        }
        // Build the substitutions array for use in the theme function.
        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,
                ];
            }
        }
    }
    // Give the area handlers a chance to extend the form.
    $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.