function FormWizardBase::actions

Same name and namespace in other branches
  1. 8.x-3.x src/Wizard/FormWizardBase.php \Drupal\ctools\Wizard\FormWizardBase::actions()

Generates action elements for navigating between the operation steps.

Parameters

\Drupal\Core\Form\FormInterface $form_object: The current operation form.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

Return value

array

1 call to FormWizardBase::actions()
FormWizardBase::buildForm in src/Wizard/FormWizardBase.php
Form constructor.

File

src/Wizard/FormWizardBase.php, line 364

Class

FormWizardBase
The base class for all form wizard.

Namespace

Drupal\ctools\Wizard

Code

protected function actions(FormInterface $form_object, FormStateInterface $form_state) {
  $cached_values = $form_state->getTemporaryValue('wizard');
  $operations = $this->getOperations($cached_values);
  $step = $this->getStep($cached_values);
  $operation = $operations[$step];
  $steps = array_keys($operations);
  // Slice to find the operations that occur before the current operation.
  $before = array_slice($operations, 0, array_search($step, $steps));
  // Slice to find the operations that occur after the current operation.
  $after = array_slice($operations, array_search($step, $steps) + 1);
  $actions = [
    '#type' => 'actions',
    'submit' => [
      '#type' => 'submit',
      '#value' => $this->t('Next'),
      '#button_type' => 'primary',
      '#validate' => [
        '::populateCachedValues',
        [
          $form_object,
          'validateForm',
        ],
      ],
      '#submit' => [
        [
          $form_object,
          'submitForm',
        ],
      ],
    ],
  ];
  // Add any submit or validate functions for the step and the global ones.
  if (isset($operation['validate'])) {
    $actions['submit']['#validate'] = array_merge($actions['submit']['#validate'], $operation['validate']);
  }
  $actions['submit']['#validate'][] = '::validateForm';
  if (isset($operation['submit'])) {
    $actions['submit']['#submit'] = array_merge($actions['submit']['#submit'], $operation['submit']);
  }
  $actions['submit']['#submit'][] = '::submitForm';
  if ($form_state->get('ajax')) {
    // Ajax submissions need to submit to the current step, not "next".
    $parameters = $this->getNextParameters($cached_values);
    $parameters['step'] = $this->getStep($cached_values);
    $actions['submit']['#ajax'] = [
      'callback' => '::ajaxSubmit',
      'url' => Url::fromRoute($this->getRouteName(), $parameters),
      'options' => [
        'query' => $this->getRequest()->query
          ->all() + [
          FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        ],
      ],
    ];
  }
  // If there are steps before this one, label the button "previous"
  // otherwise do not display a button.
  if ($before) {
    $actions['previous'] = [
      '#type' => 'submit',
      '#value' => $this->t('Previous'),
      '#validate' => [
        [
          $this,
          'populateCachedValues',
        ],
      ],
      '#submit' => [
        [
          $this,
          'previous',
        ],
      ],
      '#limit_validation_errors' => [],
      '#weight' => -10,
    ];
    if ($form_state->get('ajax')) {
      // Ajax submissions need to submit to the current step, not "previous".
      $parameters = $this->getPreviousParameters($cached_values);
      $parameters['step'] = $this->getStep($cached_values);
      $actions['previous']['#ajax'] = [
        'callback' => '::ajaxPrevious',
        'url' => Url::fromRoute($this->getRouteName(), $parameters),
        'options' => [
          'query' => $this->getRequest()->query
            ->all() + [
            FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
          ],
        ],
      ];
    }
  }
  // If there are not steps after this one, label the button "Finish".
  if (!$after) {
    $actions['submit']['#value'] = $this->t('Finish');
    $actions['submit']['#submit'][] = [
      $this,
      'finish',
    ];
    if ($form_state->get('ajax')) {
      $actions['submit']['#ajax']['callback'] = [
        $this,
        'ajaxFinish',
      ];
    }
  }
  return $actions;
}