function ViewEditForm::form

Same name in other branches
  1. 8.9.x core/modules/views_ui/src/ViewEditForm.php \Drupal\views_ui\ViewEditForm::form()
  2. 10 core/modules/views_ui/src/ViewEditForm.php \Drupal\views_ui\ViewEditForm::form()
  3. 11.x core/modules/views_ui/src/ViewEditForm.php \Drupal\views_ui\ViewEditForm::form()

Overrides EntityForm::form

File

core/modules/views_ui/src/ViewEditForm.php, line 106

Class

ViewEditForm
Form controller for the Views edit form.

Namespace

Drupal\views_ui

Code

public function form(array $form, FormStateInterface $form_state) {
    
    /** @var \Drupal\views_ui\ViewUI $view */
    $view = $this->entity;
    $display_id = $this->displayID;
    // Do not allow the form to be cached, because $form_state->get('view') can become
    // stale between page requests.
    // See views_ui_ajax_get_form() for how this affects #ajax.
    // @todo To remove this and allow the form to be cacheable:
    //   - Change $form_state->get('view') to $form_state->getTemporary()['view'].
    //   - Add a #process function to initialize $form_state->getTemporary()['view']
    //     on cached form submissions.
    //   - Use \Drupal\Core\Form\FormStateInterface::loadInclude().
    $form_state->disableCache();
    if ($display_id) {
        if (!$view->getExecutable()
            ->setDisplay($display_id)) {
            $form['#markup'] = $this->t('Invalid display id @display', [
                '@display' => $display_id,
            ]);
            return $form;
        }
    }
    $form['#tree'] = TRUE;
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $form['#attached']['library'][] = 'core/drupal.states';
    $form['#attached']['library'][] = 'core/drupal.tabledrag';
    $form['#attached']['library'][] = 'views_ui/views_ui.admin';
    $form['#attached']['library'][] = 'views_ui/admin.styling';
    $form += [
        '#prefix' => '',
        '#suffix' => '',
    ];
    $view_status = $view->status() ? 'enabled' : 'disabled';
    $form['#prefix'] .= '<div class="views-edit-view views-admin ' . $view_status . ' clearfix">';
    $form['#suffix'] = '</div>' . $form['#suffix'];
    $form['#attributes']['class'] = [
        'form-edit',
    ];
    if ($view->isLocked()) {
        $form['locked'] = [
            '#type' => 'container',
            '#attributes' => [
                'class' => [
                    'view-locked',
                    'messages',
                    'messages--warning',
                ],
            ],
            '#weight' => -10,
            'message' => [
                '#type' => 'break_lock_link',
                '#label' => $view->getEntityType()
                    ->getSingularLabel(),
                '#lock' => $view->getLock(),
                '#url' => $view->toUrl('break-lock-form'),
            ],
        ];
    }
    else {
        $form['changed'] = [
            '#type' => 'container',
            '#attributes' => [
                'class' => [
                    'view-changed',
                    'messages',
                    'messages--warning',
                ],
            ],
            '#children' => $this->t('You have unsaved changes.'),
            '#weight' => -10,
        ];
        if (empty($view->changed)) {
            $form['changed']['#attributes']['class'][] = 'js-hide';
        }
    }
    $form['displays'] = [
        '#prefix' => '<h1 class="unit-title clearfix">' . $this->t('Displays') . '</h1>',
        '#type' => 'container',
        '#attributes' => [
            'class' => [
                'views-displays',
            ],
        ],
    ];
    $form['displays']['top'] = $this->renderDisplayTop($view);
    // The rest requires a display to be selected.
    if ($display_id) {
        $form_state->set('display_id', $display_id);
        // The part of the page where editing will take place.
        $form['displays']['settings'] = [
            '#type' => 'container',
            '#id' => 'edit-display-settings',
            '#attributes' => [
                'class' => [
                    'edit-display-settings',
                ],
            ],
        ];
        // Add a text that the display is disabled.
        if ($view->getExecutable()->displayHandlers
            ->has($display_id)) {
            if (!$view->getExecutable()->displayHandlers
                ->get($display_id)
                ->isEnabled()) {
                $form['displays']['settings']['disabled']['#markup'] = $this->t('This display is disabled.');
            }
        }
        // Add the edit display content
        $tab_content = $this->getDisplayTab($view);
        $tab_content['#theme_wrappers'] = [
            'container',
        ];
        $tab_content['#attributes'] = [
            'class' => [
                'views-display-tab',
            ],
        ];
        $tab_content['#id'] = 'views-tab-' . $display_id;
        // Mark deleted displays as such.
        $display = $view->get('display');
        if (!empty($display[$display_id]['deleted'])) {
            $tab_content['#attributes']['class'][] = 'views-display-deleted';
        }
        // Mark disabled displays as such.
        if ($view->getExecutable()->displayHandlers
            ->has($display_id) && !$view->getExecutable()->displayHandlers
            ->get($display_id)
            ->isEnabled()) {
            $tab_content['#attributes']['class'][] = 'views-display-disabled';
        }
        $form['displays']['settings']['settings_content'] = [
            '#type' => 'container',
            'tab_content' => $tab_content,
        ];
    }
    return $form;
}

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