You are here

public function FeedForm::save in Feeds 8.3

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/FeedForm.php, line 192

Class

FeedForm
Form controller for the feed edit forms.

Namespace

Drupal\feeds

Code

public function save(array $form, FormStateInterface $form_state) {
  $feed = $this->entity;
  $insert = $feed
    ->isNew();
  $feed
    ->save();
  $context = [
    '@type' => $feed
      ->bundle(),
    '%title' => $feed
      ->label(),
  ];
  $t_args = [
    '@type' => $feed
      ->getType()
      ->label(),
    '%title' => $feed
      ->label(),
  ];
  if ($insert) {
    $this
      ->logger('feeds')
      ->notice('@type: added %title.', $context);
    $this
      ->messenger()
      ->addMessage($this
      ->t('%title has been created.', $t_args));
  }
  else {
    $this
      ->logger('feeds')
      ->notice('@type: updated %title.', $context);
    $this
      ->messenger()
      ->addMessage($this
      ->t('%title has been updated.', $t_args));
  }
  if (!$feed
    ->id()) {

    // In the unlikely case something went wrong on save, the feed will be
    // rebuilt and feed form redisplayed the same way as in preview.
    $this
      ->messenger()
      ->addError($this
      ->t('The feed could not be saved.'));
    $form_state
      ->setRebuild();
    return;
  }
  if ($feed
    ->access('view')) {
    $form_state
      ->setRedirect('entity.feeds_feed.canonical', [
      'feeds_feed' => $feed
        ->id(),
    ]);
  }
  else {
    $form_state
      ->setRedirect('<front>');
  }
}