You are here

function workflow_form_alter in Workflow 8

Same name and namespace in other branches
  1. 5.2 workflow.module \workflow_form_alter()
  2. 5 workflow.module \workflow_form_alter()
  3. 6.2 workflow.module \workflow_form_alter()
  4. 6 workflow.module \workflow_form_alter()
  5. 7.2 workflow.form.inc \workflow_form_alter()

Implements hook_form_alter().

Form builder. Move action buttons next to the 'Save'/'Delete' buttons.

This is only used if the set the 'options widget' to 'action buttons'. Do not use with multiple workflows per entity: confusing UX. ATM this works for:

  • Workflow Field: create, edit, view, workflow tab, comment;
  • Workflow Node: view, workflow tab;

(For forms with Workflow Node, the form_alter() is AFTER formElement(). )

@todo Move this to WorkflowTransitionForm::_addActionButtons();

File

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

Code

function workflow_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // N.B. Keep code aligned: workflow_form_alter(), WorkflowTransitionForm::actions().
  // Use a fast, custom way to check if we need to do this.
  // @todo Make this work with multiple workflows per entity.
  if (!_workflow_use_action_buttons()) {
    return;
  }
  if (isset($form_state
    ->getBuildInfo()['base_form_id']) && $form_state
    ->getBuildInfo()['base_form_id'] == 'workflow_transition_form') {

    // The WorkflowTransitionForm::actions() has its own handling.
    // E.g., Workflow History tab, Block.
    return;
  }

  // Find the first workflow.
  // (So this won't work with multiple workflows per entity.)
  $workflow_element = _workflow_transition_form_get_first_workflow_element($form);

  // Quit if there is no Workflow on this page.
  if (!$workflow_element) {
    return;
  }

  // Quit if there are no Workflow Action buttons.
  // (If user has only 1 workflow option, there are no Action buttons.)
  if (count($workflow_element['to_sid']['#options']) <= 1) {
    return;
  }

  // Find the default submit button and replace with our own action buttons.
  if (isset($form['actions']['submit'])) {
    $default_submit_action = $form['actions']['submit'];

    // @todo D8: On Node-form, this is not sufficient.
    unset($form['actions']['submit']);
  }
  elseif (isset($form['actions']['save'])) {
    $default_submit_action = $form['actions']['save'];
    unset($form['actions']['save']);
  }
  else {

    // @todo test: when does this happen?
    $default_submit_action = [];
  }
  if (isset($default_submit_action)) {
    $actions = _workflow_transition_form_get_action_buttons($form, $workflow_element, $default_submit_action);

    // Place the button with the other action buttons.
    $form['actions'] = isset($form['actions']) ? $form['actions'] : [];
    $form['actions'] += $actions;
  }
}