You are here

public function OgMenuInstanceForm::save in Organic Groups Menu (OG Menu) 8

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/OgMenuInstanceForm.php, line 373

Class

OgMenuInstanceForm
Form controller for OG Menu instance edit forms.

Namespace

Drupal\og_menu\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $menu = $this->entity;
  if (!$menu
    ->isNew() || $menu
    ->isLocked()) {
    $this
      ->submitOverviewForm($form, $form_state);
  }
  $status = $menu
    ->save();
  switch ($status) {
    case SAVED_NEW:
      $this
        ->messenger()
        ->addMessage($this
        ->t('Created the %label menu.', [
        '%label' => $menu
          ->label(),
      ]));
      break;
    default:
      $this
        ->messenger()
        ->addMessage($this
        ->t('Saved the %label menu.', [
        '%label' => $menu
          ->label(),
      ]));
  }
  $field_storage = FieldStorageConfig::loadByName($this->entity
    ->getEntityTypeId(), OgGroupAudienceHelperInterface::DEFAULT_FIELD);
  $target_type = $field_storage
    ->getSetting('target_type');
  $group_entity = $this->entity
    ->getGroup();

  // If possible, redirect to the group after save.
  if ($target_type && $group_entity) {
    $form_state
      ->setRedirect('entity.' . $target_type . '.canonical', [
      $target_type => $group_entity
        ->id(),
    ]);
  }
  else {
    $form_state
      ->setRedirect('entity.ogmenu_instance.edit_form', [
      'ogmenu_instance' => $menu
        ->id(),
    ]);
  }
}