function ViewUI::getStandardButtons

Same name in other branches
  1. 9 core/modules/views_ui/src/ViewUI.php \Drupal\views_ui\ViewUI::getStandardButtons()
  2. 10 core/modules/views_ui/src/ViewUI.php \Drupal\views_ui\ViewUI::getStandardButtons()
  3. 11.x core/modules/views_ui/src/ViewUI.php \Drupal\views_ui\ViewUI::getStandardButtons()

Provide a standard set of Apply/Cancel/OK buttons for the forms. Also provide a hidden op operator because the forms plugin doesn't seem to properly provide which button was clicked.

TODO: Is the hidden op operator still here somewhere, or is that part of the docblock outdated?

File

core/modules/views_ui/src/ViewUI.php, line 282

Class

ViewUI
Stores UI related temporary settings.

Namespace

Drupal\views_ui

Code

public function getStandardButtons(&$form, FormStateInterface $form_state, $form_id, $name = NULL) {
    $form['actions'] = [
        '#type' => 'actions',
    ];
    if (empty($name)) {
        $name = t('Apply');
        if (!empty($this->stack) && count($this->stack) > 1) {
            $name = t('Apply and continue');
        }
        $names = [
            t('Apply'),
            t('Apply and continue'),
        ];
    }
    // Forms that are purely informational set an ok_button flag, so we know not
    // to create an "Apply" button for them.
    if (!$form_state->get('ok_button')) {
        $form['actions']['submit'] = [
            '#type' => 'submit',
            '#value' => $name,
            '#id' => 'edit-submit-' . Html::getUniqueId($form_id),
            // The regular submit handler ($form_id . '_submit') does not apply if
            // we're updating the default display. It does apply if we're updating
            // the current display. Since we have no way of knowing at this point
            // which display the user wants to update, views_ui_standard_submit will
            // take care of running the regular submit handler as appropriate.
'#submit' => [
                [
                    $this,
                    'standardSubmit',
                ],
            ],
            '#button_type' => 'primary',
        ];
        // Form API button click detection requires the button's #value to be the
        // same between the form build of the initial page request, and the
        // initial form build of the request processing the form submission.
        // Ideally, the button's #value shouldn't change until the form rebuild
        // step. However, \Drupal\views_ui\Form\Ajax\ViewsFormBase::getForm()
        // implements a different multistep form workflow than the Form API does,
        // and adjusts $view->stack prior to form processing, so we compensate by
        // extending button click detection code to support any of the possible
        // button labels.
        if (isset($names)) {
            $form['actions']['submit']['#values'] = $names;
            $form['actions']['submit']['#process'] = array_merge([
                'views_ui_form_button_was_clicked',
            ], \Drupal::service('element_info')->getInfoProperty($form['actions']['submit']['#type'], '#process', []));
        }
        // If a validation handler exists for the form, assign it to this button.
        $form['actions']['submit']['#validate'][] = [
            $form_state->getFormObject(),
            'validateForm',
        ];
    }
    // Create a "Cancel" button. For purely informational forms, label it "OK".
    $cancel_submit = function_exists($form_id . '_cancel') ? $form_id . '_cancel' : [
        $this,
        'standardCancel',
    ];
    $form['actions']['cancel'] = [
        '#type' => 'submit',
        '#value' => !$form_state->get('ok_button') ? t('Cancel') : t('Ok'),
        '#submit' => [
            $cancel_submit,
        ],
        '#validate' => [],
        '#limit_validation_errors' => [],
    ];
    // Compatibility, to be removed later: // TODO: When is "later"?
    // We used to set these items on the form, but now we want them on the $form_state:
    if (isset($form['#title'])) {
        $form_state->set('title', $form['#title']);
    }
    if (isset($form['#section'])) {
        $form_state->set('#section', $form['#section']);
    }
    // Finally, we never want these cached -- our object cache does that for us.
    $form['#no_cache'] = TRUE;
}

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