You are here

public function SlideBaseForm::save in Drupal Slider 8.2

Same name and namespace in other branches
  1. 8 src/Form/SlideBaseForm.php \Drupal\drupal_slider\Form\SlideBaseForm::save()

Function save.

Overrides EntityForm::save

File

src/Form/SlideBaseForm.php, line 160

Class

SlideBaseForm
Class SlideBaseForm.

Namespace

Drupal\drupal_slider\Form

Code

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

  // EntityForm provides us with the entity we're working on.
  $slide = $this
    ->getEntity();

  // Drupal already populated the form values in the entity object. Each
  // form field was saved as a public variable in the entity class. PHP
  // allows Drupal to do this even if the method is not defined ahead of
  // time.
  $status = $slide
    ->save();

  // Grab the URL of the new entity. We'll use it in the message.
  $url = $slide
    ->toUrl();

  // Create an edit link.
  $edit_link = Link::fromTextAndUrl($this
    ->t('Edit'), $url)
    ->toString();
  if ($status == SAVED_UPDATED) {

    // If we edited an existing entity...
    $this
      ->messenger()
      ->addMessage($this
      ->t('Slide %label has been updated.', [
      '%label' => $slide
        ->label(),
    ]));
    $this
      ->logger('contact')
      ->notice('Slide %label has been updated.', [
      '%label' => $slide
        ->label(),
      'link' => $edit_link,
    ]);
  }
  else {

    // If we created a new entity...
    $this
      ->messenger()
      ->addMessage($this
      ->t('Slide %label has been added.', [
      '%label' => $slide
        ->label(),
    ]));
    $this
      ->logger('contact')
      ->notice('Slide %label has been added.', [
      '%label' => $slide
        ->label(),
      'link' => $edit_link,
    ]);
  }

  // Redirect the user back to the listing route after the save operation.
  $form_state
    ->setRedirect('entity.drupal_slider_slide.list');
}