You are here

protected function YamlFormSubmissionForm::actions in YAML Form 8

Returns an array of supported actions for the current entity form.

This function generates a list of Form API elements which represent actions supported by the current entity form.

@todo Consider introducing a 'preview' action here, since it is used by many entity types.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array An array of supported Form API action elements keyed by name.

Overrides EntityForm::actions

File

src/YamlFormSubmissionForm.php, line 512

Class

YamlFormSubmissionForm
Provides a form to collect and edit submissions.

Namespace

Drupal\yamlform

Code

protected function actions(array $form, FormStateInterface $form_state) {

  /* @var $yamlform_submission \Drupal\yamlform\YamlFormSubmissionInterface */
  $yamlform_submission = $this->entity;
  $yamlform = $this
    ->getYamlForm();
  $element = parent::actions($form, $form_state);

  /* @var \Drupal\yamlform\YamlFormSubmissionInterface $yamlform_submission */
  $preview_mode = $this
    ->getYamlFormSetting('preview');

  // Remove the delete button from the form submission form.
  unset($element['delete']);

  // Mark the submit action as the primary action, when it appears.
  $element['submit']['#button_type'] = 'primary';
  $element['submit']['#attributes'] = $this
    ->getYamlFormSetting('form_submit_attributes');
  $element['submit']['#attributes']['class'][] = 'yamlform-button--submit';

  // Customize the submit button's label for new submissions only.
  if ($yamlform_submission
    ->isNew() || $yamlform_submission
    ->isDraft()) {
    $element['submit']['#value'] = $this
      ->getYamlFormSetting('form_submit_label');
  }

  // Add validate and complete handler to submit.
  $element['submit']['#validate'][] = '::validateForm';
  $element['submit']['#validate'][] = '::autosave';
  $element['submit']['#validate'][] = '::complete';

  // Add confirm(ation) handler to submit button.
  $element['submit']['#submit'][] = '::confirmForm';
  $pages = $this
    ->getPages($form, $form_state);
  $current_page = $this
    ->getCurrentPage($form, $form_state);
  if ($pages) {

    // Get current page element which can contain custom prev(ious) and next button
    // labels.
    $current_page_element = $this
      ->getYamlForm()
      ->getPage($current_page);
    $is_first_page = $current_page == $this
      ->getFirstPage($form, $form_state) ? TRUE : FALSE;
    $is_last_page = in_array($current_page, [
      'preview',
      'complete',
      $this
        ->getLastPage($form, $form_state),
    ]) ? TRUE : FALSE;
    $is_preview_page = $current_page == 'preview';
    $is_next_page_preview = $this
      ->getNextPage($form, $form_state) == 'preview' ? TRUE : FALSE;
    $is_next_page_optional_preview = $is_next_page_preview && $preview_mode != DRUPAL_REQUIRED;

    // Only show that save button if this is the last page of the wizard or
    // on preview page or right before the optional preview.
    $element['submit']['#access'] = $is_last_page || $is_preview_page || $is_next_page_optional_preview;
    if (!$is_first_page) {
      if ($is_preview_page) {
        $previous_attributes = $this
          ->getYamlFormSetting('preview_prev_button_attributes');
        $previous_label = $this
          ->getYamlFormSetting('preview_prev_button_label');
      }
      else {
        $previous_attributes = $this
          ->getYamlFormSetting('wizard_prev_button_attributes');
        $previous_label = isset($current_page_element['#prev_button_label']) ? $current_page_element['#prev_button_label'] : $this
          ->getYamlFormSetting('wizard_prev_button_label');
      }
      $previous_attributes['class'][] = 'js-yamlform-novalidate';
      $previous_attributes['class'][] = 'yamlform-button--previous';
      $element['previous'] = [
        '#type' => 'submit',
        '#value' => $previous_label,
        '#validate' => [
          '::noValidate',
        ],
        '#submit' => [
          '::previous',
        ],
        '#attributes' => $previous_attributes,
      ];
    }
    if (!$is_last_page) {
      if ($is_next_page_preview) {
        $next_attributes = $this
          ->getYamlFormSetting('preview_next_button_attributes');
        $next_label = $this
          ->getYamlFormSetting('preview_next_button_label');
        $next_attributes['class'][] = 'yamlform-button--preview';
      }
      else {
        $next_attributes = $this
          ->getYamlFormSetting('wizard_next_button_attributes');
        $next_label = isset($current_page_element['#next_button_label']) ? $current_page_element['#next_button_label'] : $this
          ->getYamlFormSetting('wizard_next_button_label');
        $next_attributes['class'][] = 'yamlform-button--next';
      }
      $element['next'] = [
        '#type' => 'submit',
        '#value' => $next_label,
        '#validate' => [
          '::validateForm',
        ],
        '#submit' => [
          '::next',
        ],
        '#attributes' => $next_attributes,
      ];
    }
  }

  // Draft.
  if ($this
    ->draftEnabled()) {
    $draft_attributes = $this
      ->getYamlFormSetting('draft_button_attributes');
    $draft_attributes['class'][] = 'yamlform-button--draft';
    $element['draft'] = [
      '#type' => 'submit',
      '#value' => $this
        ->getYamlFormSetting('draft_button_label'),
      '#validate' => [
        '::draft',
      ],
      '#submit' => [
        '::submitForm',
        '::save',
        '::rebuild',
      ],
      '#attributes' => $draft_attributes,
    ];
  }
  return $element;
}