You are here

public function WorkspaceForm::form in Workspace 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

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

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

File

src/Entity/Form/WorkspaceForm.php, line 64

Class

WorkspaceForm
Form controller for the workspace edit forms.

Namespace

Drupal\workspace\Entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $workspace = $this->entity;
  if ($this->operation == 'edit') {

    // Allow the user to not abort on conflicts.
    $this->conflictTracker
      ->useWorkspace($workspace);
    $conflicts = $this->conflictTracker
      ->getAll();
    if ($conflicts) {
      $form['message'] = $this
        ->generateMessageRenderArray('error', $this
        ->t('There are <a href=":link">@count conflict(s) with the :target workspace</a>. Pushing changes to :target may result in unexpected behavior or data loss, and cannot be undone. Please proceed with caution.', [
        '@count' => count($conflicts),
        ':link' => Url::fromRoute('entity.workspace.conflicts', [
          'workspace' => $workspace
            ->id(),
        ])
          ->toString(),
        ':target' => $workspace
          ->get('upstream')->entity ? $workspace
          ->get('upstream')->entity
          ->label() : '',
      ]));
      $form['is_aborted_on_conflict'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t('Abort if there are conflicts?'),
        '#default_value' => 'true',
        '#options' => [
          'true' => $this
            ->t('Yes, if conflicts are found do not replicate to upstream.'),
          'false' => $this
            ->t('No, go ahead and push any conflicts to the upstream.'),
        ],
        '#weight' => 0,
      ];
    }
    else {
      $form['message'] = $this
        ->generateMessageRenderArray('status', 'There are no conflicts.');
    }

    // Set the form title based on workspace.
    $form['#title'] = $this
      ->t('Edit workspace %label', [
      '%label' => $workspace
        ->label(),
    ]);
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 128,
    '#default_value' => $workspace
      ->label(),
    '#description' => $this
      ->t("Label for the Workspace."),
    '#required' => TRUE,
    '#weight' => 1,
  ];
  $form['machine_name'] = [
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#title' => $this
      ->t('Workspace Machine Name'),
    '#maxlength' => 128,
    '#default_value' => $workspace
      ->get('machine_name')->value,
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
    ],
  ];
  return parent::form($form, $form_state, $workspace);
}