You are here

public function PoolForm::save in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/Form/PoolForm.php \Drupal\cms_content_sync\Form\PoolForm::save()
  2. 2.0.x src/Form/PoolForm.php \Drupal\cms_content_sync\Form\PoolForm::save()

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/Form/PoolForm.php, line 258

Class

PoolForm
Form handler for the Pool add and edit forms.

Namespace

Drupal\cms_content_sync\Form

Code

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

  /**
   * @var \Drupal\cms_content_sync\Entity\Pool $pool
   */
  $pool = $this->entity;
  if (!$pool
    ->label()) {
    $pool->label = $this
      ->getRemotePools($form_state)[$pool
      ->id()];
  }
  $status = $pool
    ->save();
  if ($status) {
    if (empty(Flow::getAll())) {
      $link = Link::createFromRoute($this
        ->t('Create a Flow'), 'entity.cms_content_sync_flow.add_form')
        ->toString();
      \Drupal::messenger()
        ->addStatus($this
        ->t('Saved Pool %label. Well done! @create to continue the setup.', [
        '%label' => $pool
          ->label(),
        '@create' => $link,
      ]));
      \Drupal::messenger()
        ->addStatus($this
        ->t('If you have connected another site already, @copy. Mirroring means you can simply swap the push and pull settings.', [
        '@copy' => Link::createFromRoute('copy or mirror the configuration from another site', 'entity.cms_content_sync_flow.copy_remote')
          ->toString(),
      ]));
    }
    else {
      \Drupal::messenger()
        ->addStatus($this
        ->t('Saved Pool %label.', [
        '%label' => $pool
          ->label(),
      ]));
    }
  }
  else {
    \Drupal::messenger()
      ->addStatus($this
      ->t('The %label Pool could not be saved.', [
      '%label' => $pool
        ->label(),
    ]));
  }

  // Make sure that the export is executed.
  $destination = \Drupal::request()->query
    ->get('destination');
  \Drupal::request()->query
    ->remove('destination');

  // Keep destination for after the export.
  $options = [];
  if (!empty($destination)) {
    $options['query']['destination'] = $destination;
  }
  $form_state
    ->setRedirect('entity.cms_content_sync_pool.export', [
    'cms_content_sync_pool' => $this->entity
      ->id(),
  ], $options);
}