You are here

public function FormBase::save in Linkit 8.4

Same name and namespace in other branches
  1. 8.5 src/Form/Profile/FormBase.php \Drupal\linkit\Form\Profile\FormBase::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/Profile/FormBase.php, line 65
Contains \Drupal\linkit\Form\Profile\FormBase.

Class

FormBase
Base form for profile add and edit forms.

Namespace

Drupal\linkit\Form\Profile

Code

public function save(array $form, FormStateInterface $form_state) {
  $linkit_profile = $this->entity;

  // Prevent leading and trailing spaces in linkit profile labels.
  $linkit_profile
    ->set('label', trim($linkit_profile
    ->label()));
  $status = $linkit_profile
    ->save();
  $edit_link = $this->entity
    ->link($this
    ->t('Edit'));
  switch ($status) {
    case SAVED_NEW:
      drupal_set_message($this
        ->t('Created new profile %label.', [
        '%label' => $linkit_profile
          ->label(),
      ]));
      $this
        ->logger('linkit')
        ->notice('Created new profile %label.', [
        '%label' => $linkit_profile
          ->label(),
        'link' => $edit_link,
      ]);
      $form_state
        ->setRedirect('linkit.matchers', [
        'linkit_profile' => $linkit_profile
          ->id(),
      ]);
      break;
    case SAVED_UPDATED:
      drupal_set_message($this
        ->t('Updated profile %label.', [
        '%label' => $linkit_profile
          ->label(),
      ]));
      $this
        ->logger('linkit')
        ->notice('Updated profile %label.', [
        '%label' => $linkit_profile
          ->label(),
        'link' => $edit_link,
      ]);
      $form_state
        ->setRedirectUrl($linkit_profile
        ->urlInfo('edit-form'));
      break;
  }
}