You are here

public function FormsStepsProgressStepEditForm::form in Forms Steps 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

src/Form/FormsStepsProgressStepEditForm.php, line 40

Class

FormsStepsProgressStepEditForm
Class FormsStepsProgressStepEditForm.

Namespace

Drupal\forms_steps\Form

Code

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

  /* @var \Drupal\forms_steps\FormsStepsInterface $forms_steps */
  $forms_steps = $this
    ->getEntity();
  $progress_step = $forms_steps
    ->getProgressStep($this->progressStepId);
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $progress_step
      ->label(),
    '#description' => $this
      ->t('Label for the progress step.'),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
    ],
    '#default_value' => $progress_step
      ->id(),
    '#disabled' => TRUE,
  ];
  $steps = $forms_steps
    ->getSteps();

  // Warn the user if there are no steps.
  if (empty($steps)) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('This Forms Steps has no steps and will be disabled until there is at least one, <a href=":add-step">add a new step.</a>', [
      ':add-step' => $forms_steps
        ->toUrl('add-step-form')
        ->toString(),
    ]));
  }

  // [$this->t('There are no steps yet.')].
  $options = [];
  foreach ($steps as $step) {
    $options[$step
      ->id()] = $step
      ->label();
  }
  if (!empty($steps)) {
    $form['routes'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Steps of activation'),
      '#description' => $this
        ->t('Select the steps for which the current progress step should be active.'),
      '#required' => TRUE,
      '#options' => $options,
      '#default_value' => $progress_step
        ->activeRoutes(),
    ];
    $form['link'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Link'),
      '#description' => $this
        ->t('Select the step for which the current progress step should redirect on click. Leave empty for no link on this progress step.'),
      '#empty_option' => $this
        ->t('- None -'),
      '#options' => $options,
      '#default_value' => $progress_step
        ->link(),
    ];
    $form['link_visibility'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Link visibility'),
      '#description' => $this
        ->t('Select the steps for which the link will be shown.'),
      '#options' => $options,
      '#states' => [
        'invisible' => [
          ':input[name="link"]' => [
            'value' => '',
          ],
        ],
      ],
      '#default_value' => $progress_step
        ->linkVisibility(),
    ];
  }
  return $form;
}