You are here

public function WorkspaceForm::save in Workspace 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/WorkspaceForm.php, line 154

Class

WorkspaceForm
Form controller for the workspace edit forms.

Namespace

Drupal\workspace\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
  $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);
  $workspace = $this->entity;
  $is_new = $workspace
    ->isNew();
  $workspace
    ->save();
  $info = [
    '%info' => $workspace
      ->label(),
  ];
  $context = [
    '@type' => $workspace
      ->bundle(),
    '%info' => $workspace
      ->label(),
  ];
  $logger = $this
    ->logger('workspace');

  // If Workbench Moderation is enabled, a publish of the Workspace should
  // trigger a replication. We pass back the status of that replication using
  // a static variable. If replication happened, we want to handle the case
  // of failed replication, as well as modify the wording of the saved
  // message.
  // @see \Drupal\workspace\EventSubscriber\WorkbenchModerationSubscriber
  // @todo Avoid using statics.
  $replication_status = drupal_static('publish_workspace_replication_status', NULL);
  if ($replication_status !== NULL) {
    $logger
      ->notice('@type: updated %info.', $context);
    if ($replication_status == TRUE) {

      // The replication succeeded, in addition to saving the workspace.
      drupal_set_message($this
        ->t('Workspace :source has been updated and changes were pushed to :target.', [
        ':source' => $workspace
          ->label(),
        ':target' => $workspace
          ->get('upstream')->entity
          ->label(),
      ]), 'status');
      $form_state
        ->setValue('id', $workspace
        ->id());
      $form_state
        ->set('id', $workspace
        ->id());
      $redirect = $this
        ->currentUser()
        ->hasPermission('administer workspaces') ? $workspace
        ->toUrl('collection') : $workspace
        ->toUrl('canonical');
      $form_state
        ->setRedirectUrl($redirect);
    }
    else {

      // The replication failed, even though the Workspace was updated.
      $previous_workflow_state = drupal_static('publish_workspace_previous_state', NULL);

      // This variable should always be set, else there is an issue with
      // the trigger logic.
      if ($previous_workflow_state === NULL) {
        throw new \Exception('The publish_workspace_replication_status should be set.');
      }

      // Revert the workspace back to its previous moderation state.
      $workspace->moderation_state->target_id = $previous_workflow_state;
      $workspace
        ->save();

      // Show the form again.
      $form_state
        ->setRebuild();
    }
  }
  else {

    // Assume a replication did not happen OR that Workbench Moderation is not
    // installed.
    if ($is_new) {
      $logger
        ->notice('@type: added %info.', $context);
      drupal_set_message($this
        ->t('Workspace %info has been created.', $info));
    }
    else {
      $logger
        ->notice('@type: updated %info.', $context);
      drupal_set_message($this
        ->t('Workspace %info has been updated.', $info));
    }
    if ($workspace
      ->id()) {
      $form_state
        ->setValue('id', $workspace
        ->id());
      $form_state
        ->set('id', $workspace
        ->id());
      $redirect = $this
        ->currentUser()
        ->hasPermission('administer workspaces') ? $workspace
        ->toUrl('collection') : $workspace
        ->toUrl('canonical');
      $form_state
        ->setRedirectUrl($redirect);
    }
    else {
      drupal_set_message($this
        ->t('The workspace could not be saved.'), 'error');
      $form_state
        ->setRebuild();
    }
  }
}