You are here

public function ReplicationForm::buildForm in Deploy - Content Staging 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 EntityForm::buildForm

File

src/Entity/Form/ReplicationForm.php, line 70

Class

ReplicationForm
Form controller for Replication edit forms.

Namespace

Drupal\deploy\Entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $input = $form_state
    ->getUserInput();
  $js = isset($input['_drupal_ajax']) ? TRUE : FALSE;
  $form = parent::buildForm($form, $form_state);
  $default_source = $this
    ->getDefaultSource();
  $default_target = $this
    ->getDefaultTarget();

  // For remote targets the remote workspace can't be loaded from workspace
  // pointer.
  $published = NULL;

  /** @var \Drupal\multiversion\Entity\WorkspaceInterface|NULL $target_workspace */
  if ($target_workspace = $default_target
    ->get('workspace_pointer')->entity) {
    $published = $target_workspace
      ->isPublished();
  }
  if (!$default_source || !$default_target || $published === FALSE) {
    $message = $this
      ->t('Source and target must be set, make sure your current workspace has an upstream. Go to <a href=":path">this page</a> to edit your workspaces.', [
      ':path' => Url::fromRoute('entity.workspace.collection')
        ->toString(),
    ]);
    if ($js) {
      return [
        '#markup' => $message,
      ];
    }
    $this
      ->messenger()
      ->addError($message);
    return [];
  }

  // @todo Move this to be injected.
  $this->conflictTracker = \Drupal::service('workspace.conflict_tracker');

  // Allow the user to not abort on conflicts.
  $source_workspace = $default_source
    ->getWorkspace();
  $conflicts = $this->conflictTracker
    ->useWorkspace($source_workspace)
    ->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' => $source_workspace
          ->id(),
      ])
        ->toString(),
      ':target' => $default_target
        ->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.');
  }
  if (!$source_workspace
    ->isDefaultWorkspace()) {
    $form['archive'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Archive workspace after deployment'),
      '#description' => $this
        ->t('The workspace will be archived only if the deployment finishes with success.'),
      '#default_value' => FALSE,
    ];
  }
  $form['source']['widget']['#default_value'] = [
    $default_source
      ->id(),
  ];
  if (empty($this->entity
    ->get('target')->target_id) && $default_target) {
    $form['target']['widget']['#default_value'] = [
      $default_target
        ->id(),
    ];
  }
  if (!$form['source']['#access'] && !$form['target']['#access']) {
    $form['actions']['submit']['#value'] = $this
      ->t('Deploy to @target', [
      '@target' => $default_target
        ->label(),
    ]);
  }
  else {
    $form['actions']['submit']['#value'] = $this
      ->t('Deploy');
  }
  $form['actions']['submit']['#ajax'] = [
    'callback' => [
      $this,
      'deploy',
    ],
    'event' => 'mousedown',
    'prevent' => 'click',
    'progress' => [
      'type' => 'throbber',
      'message' => 'Deploying',
    ],
  ];
  return $form;
}