You are here

public function ReplicationForm::save in Deploy - Content Staging 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

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

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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

Class

ReplicationForm
Form controller for Replication edit forms.

Namespace

Drupal\deploy\Entity\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  // Pass the abort flag to the ReplicationManager using runtime-only state,
  // i.e. a static.
  // @see \Drupal\workspace\ReplicatorManager
  // @see \Drupal\workspace\Entity\Form\WorkspaceForm
  $is_aborted_on_conflict = !$form_state
    ->hasValue('is_aborted_on_conflict') || $form_state
    ->getValue('is_aborted_on_conflict') === 'true';
  drupal_static('workspace_is_aborted_on_conflict', $is_aborted_on_conflict);
  parent::save($form, $form_state);
  $input = $form_state
    ->getUserInput();
  $js = isset($input['_drupal_ajax']) ? TRUE : FALSE;
  try {
    $response = \Drupal::service('workspace.replicator_manager')
      ->replicate($this->entity
      ->get('source')->entity, $this->entity
      ->get('target')->entity, null, $this->entity);
    if ($response instanceof ReplicationLogInterface && $response
      ->get('ok')->value == TRUE) {
      $this->entity
        ->set('replicated', REQUEST_TIME)
        ->save();
      $this
        ->messenger()
        ->addMessage($this
        ->t('Deployment queued, refresh this page and check the status below. It might take a few minutes to complete.'));
      if ($form_state
        ->hasValue('archive') && $form_state
        ->getValue('archive') == TRUE) {
        $this->entity
          ->setArchiveSource()
          ->save();
      }
      if (!$js) {
        $form_state
          ->setRedirect('entity.replication.collection');
      }
    }
    else {
      $this
        ->messenger()
        ->addError('Deployment error. Check recent log messages for more details.', 'error');
    }
  } catch (\Exception $e) {
    watchdog_exception('Deploy', $e);
    $this
      ->messenger()
      ->addError($e
      ->getMessage(), 'error');
  }
  if (!$js) {
    $form_state
      ->setRedirect('entity.replication.collection');
  }
}