You are here

function _workflow_transition_form_get_action_buttons in Workflow 8

Returns the action buttons from the options widget.

Parameters

array $form:

array $workflow_element:

array $default_submit_action:

Return value

array $actions array

2 calls to _workflow_transition_form_get_action_buttons()
WorkflowTransitionForm::actions in src/Form/WorkflowTransitionForm.php
Returns an array of supported actions for the current entity form.
workflow_form_alter in ./workflow.form.inc
Implements hook_form_alter().

File

./workflow.form.inc, line 137
Contains helper functions for WorkflowTransitionForm.

Code

function _workflow_transition_form_get_action_buttons(array $form, array $workflow_element, array $default_submit_action) {
  $actions = [];
  $current_sid = $workflow_element['to_sid']['#default_value'];

  /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
  $transition = $workflow_element['workflow_transition']['#value'];
  $field_name = $transition
    ->getFieldName();

  // Find the default submit button and add our action buttons before it.
  // Get the min weight for our buttons.
  $option_weight = isset($default_submit_action['#weight']) ? $default_submit_action['#weight'] : 0;
  $option_weight = $option_weight - count($workflow_element['to_sid']['#options']);
  $min_weight = $option_weight;
  foreach ($workflow_element['to_sid']['#options'] as $sid => $option_name) {

    // Make the workflow button act exactly like the original submit button.
    $same_state_button = $sid == $current_sid;
    $workflow_submit_action = $default_submit_action;

    // Add target State ID and Field name, to set correct value in validate_buttons callback.
    $workflow_submit_action['#workflow'] = [
      'field_name' => $field_name,
      'to_sid' => $sid,
    ];

    // Keep option order. Put current state first.
    $workflow_submit_action['#weight'] = $same_state_button ? $min_weight : ++$option_weight;

    // Add/Overwrite some other settings.
    $workflow_submit_action['#access'] = TRUE;
    $workflow_submit_action['#value'] = $option_name;

    // Use one drop button, instead of several action buttons.
    if ('dropbutton' == _workflow_use_action_buttons()) {
      $workflow_submit_action['#dropbutton'] = 'save';
    }
    $workflow_submit_action['#attributes'] = $same_state_button ? [
      'class' => [
        'form-save-default-button',
      ],
    ] : [];
    $workflow_submit_action['#button_type'] = $same_state_button ? 'primary' : '';

    // @todo Works for node form and workflow tab, not for workflow block.

    //$workflow_submit_action['#executes_submit_callback']  = TRUE;

    // Add class to workflow button.
    $workflow_submit_action['#attributes']['class'][] = Html::getClass('workflow_button_' . $option_name);

    // Append the form's #validate function, or it won't be called upon submit,
    // because the workflow buttons have its own #validate.
    $workflow_submit_action['#validate'] = [];
    $workflow_submit_action['#validate'][] = '_workflow_transition_form_validate_buttons';
    if (isset($default_submit_action['#validate'])) {
      $workflow_submit_action['#validate'] = $default_submit_action['#validate'];
    }
    elseif (isset($form['#validate'])) {
      $workflow_submit_action['#validate'] = $form['#validate'];
    }

    // Append the submit-buttons's #submit function, or it won't be called upon submit.
    if (isset($default_submit_action['#submit'])) {
      $workflow_submit_action['#submit'] = $default_submit_action['#submit'];
    }
    elseif (isset($form['#submit'])) {
      $workflow_submit_action['#submit'] = $form['#submit'];
    }

    // Hide the same-state button in some cases.
    if ($same_state_button) {
      if (isset($form['#form_id']) && substr($form['#form_id'], 0, 24) == 'workflow_transition_form') {

        // Hide same-state-button on the transition-form (that is:
        // view page or workflow history tab) if there is nothing to do.
        // However, a Transition may be fieldable (have attached fields).
        if ($form['comment']['#access'] == FALSE) {
          $workflow_submit_action['#access'] = FALSE;
        }
      }
      elseif (isset($form['#id']) && $form['#id'] == 'comment-form') {

        // On comment-form, the button must stay, since you can comment to same state.
      }
      else {

        // On a entity edit page, the button must stay.
      }
    }

    // Place the button with the other action buttons.
    $actions['workflow_' . $sid] = $workflow_submit_action;
  }
  return $actions;
}