You are here

public function WorkflowStateListBuilder::buildForm in Workflow 8

Form constructor.

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 The form structure.

Overrides DraggableListBuilder::buildForm

File

src/WorkflowStateListBuilder.php, line 187

Class

WorkflowStateListBuilder
Defines a class to build a draggable listing of Workflow State entities.

Namespace

Drupal\workflow

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  if (!($workflow = workflow_url_get_workflow())) {
    return $form;
  }
  $form = parent::buildForm($form, $form_state);

  // Add a sticky header.
  $form[$this->entitiesKey] += [
    '#sticky' => TRUE,
  ];
  $wid = $workflow
    ->id();

  // Build select options for reassigning states.
  // We put a blank state first for validation.
  $state_options = workflow_get_workflow_state_names($wid, FALSE);

  // Is this the last state available?
  $form['#last_mohican'] = count($state_options) == 1;
  $form['entities']['#prefix'] = '<div id="states_table_wrapper">';
  $form['entities']['#suffix'] = '</div>';

  // Create a placeholder WorkflowState (It must NOT be saved to DB). Add it to the item list.
  if ($form_state
    ->getTriggeringElement() && $form_state
    ->getTriggeringElement()['#name'] === 'add_state') {
    $sid = '';
    $placeholder = $workflow
      ->createState($sid, FALSE);
    $placeholder
      ->set('label', '');
    $this->entities['placeholder'] = $placeholder;
    $form['entities']['placeholder'] = $this
      ->buildRow($placeholder);
  }

  // Rename 'submit' button.
  $form['actions']['submit']['#value'] = $this
    ->t('Save');

  // Add 'Add State' button.
  $form['actions']['add_state'] = [
    '#name' => 'add_state',
    '#type' => 'submit',
    '#value' => $this
      ->t('Add State'),
    '#ajax' => [
      'callback' => '::addStateCallback',
      'wrapper' => 'states_table_wrapper',
    ],
  ];
  return $form;
}