function WorkflowTransitionAddForm::form

Same name in other branches
  1. 8.9.x core/modules/workflows/src/Form/WorkflowTransitionAddForm.php \Drupal\workflows\Form\WorkflowTransitionAddForm::form()
  2. 10 core/modules/workflows/src/Form/WorkflowTransitionAddForm.php \Drupal\workflows\Form\WorkflowTransitionAddForm::form()
  3. 11.x core/modules/workflows/src/Form/WorkflowTransitionAddForm.php \Drupal\workflows\Form\WorkflowTransitionAddForm::form()

Overrides EntityForm::form

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.