You are here

public function FlagForListForm::save in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/Form/FlagForListForm.php \Drupal\flag_lists\Form\FlagForListForm::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/FlagForListForm.php, line 61

Class

FlagForListForm
Class FlagForListForm.

Namespace

Drupal\flag_lists\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $flag_for_list = $this->entity;
  $is_new = !$flag_for_list
    ->getOriginalId();
  $flag_for_list
    ->setOwner();
  if ($is_new) {

    // Configuration entities need an ID manually set.
    $machine_name = \Drupal::transliteration()
      ->transliterate($flag_for_list
      ->label(), LanguageInterface::LANGCODE_DEFAULT, '_');
    $flag_for_list
      ->set('id', mb_strtolower($machine_name));
    $flag_for_list
      ->set('weight', '0');
    $this
      ->messenger()
      ->addMessage(t('The %label flag list has been created.', [
      '%label' => $flag_for_list
        ->label(),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addMessage(t('Updated the %label flag list.', [
      '%label' => $flag_for_list
        ->label(),
    ]));
  }
  $flag_for_list
    ->save();

  // Redirect to edit form so we can populated colors.
  $form_state
    ->setRedirectUrl($flag_for_list
    ->toUrl('collection'));
}