You are here

public function WorkflowTransitionAddForm::form in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/workflows/src/Form/WorkflowTransitionAddForm.php \Drupal\workflows\Form\WorkflowTransitionAddForm::form()
  2. 9 core/modules/workflows/src/Form/WorkflowTransitionAddForm.php \Drupal\workflows\Form\WorkflowTransitionAddForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/workflows/src/Form/WorkflowTransitionAddForm.php, line 57

Class

WorkflowTransitionAddForm
Entity form variant for adding workflow transitions.

Namespace

Drupal\workflows\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\workflows\WorkflowInterface $workflow */
  $workflow = $this
    ->getEntity();
  $workflow_type = $workflow
    ->getTypePlugin();
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Transition label'),
    '#maxlength' => 255,
    '#default_value' => '',
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
    ],
  ];

  // @todo https://www.drupal.org/node/2830584 Add some ajax to ensure that
  //   only valid transitions are selectable.
  $states = array_map([
    State::class,
    'labelCallback',
  ], $workflow
    ->getTypePlugin()
    ->getStates());
  $form['from'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('From'),
    '#required' => TRUE,
    '#default_value' => [],
    '#options' => $states,
  ];
  $form['to'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('To'),
    '#required' => TRUE,
    '#options' => $states,
  ];

  // Add additional form fields from the workflow type plugin.
  if ($workflow_type
    ->hasFormClass(TransitionInterface::PLUGIN_FORM_KEY)) {
    $form['type_settings'] = [
      '#tree' => TRUE,
    ];
    $subform_state = SubformState::createForSubform($form['type_settings'], $form, $form_state);
    $form['type_settings'] += $this->pluginFormFactory
      ->createInstance($workflow_type, TransitionInterface::PLUGIN_FORM_KEY)
      ->buildConfigurationForm($form['type_settings'], $subform_state);
  }
  return $form;
}