You are here

public function ComponentSectionForm::save in Module Builder 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/Form/ComponentSectionForm.php, line 1659

Class

ComponentSectionForm
Generic form for entering a section of data for a component.

Namespace

Drupal\module_builder\Form

Code

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

  // dsm($module);
  $status = $module
    ->save();
  if ($status) {

    // Setting the success message.
    $this
      ->messenger()
      ->addStatus($this
      ->t('Saved the module: @name.', array(
      '@name' => $module->name,
    )));
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The @name module was not saved.', array(
      '@name' => $module->name,
    )));
  }

  // Optionally advance to next tab or go to the generate page.
  $element = $form_state
    ->getTriggeringElement();
  switch ($element['#mb_action']) {
    case 'submit':
      $operation = $this
        ->getOperation();

      // For a new module, we need to redirect to its edit form, as staying
      // put would leave on the add form.
      if ($operation == 'add') {
        $operation = 'edit';
      }

      // For an existing module, we also redirect so that changing the machine
      // name of the module goes to the new URL.
      $url = $module
        ->toUrl($operation . '-form');
      $form_state
        ->setRedirectUrl($url);
      break;
    case 'submit_next':
      $next_link = $this
        ->getNextLink();
      $url = $module
        ->toUrl($next_link);
      $form_state
        ->setRedirectUrl($url);
      break;
    case 'submit_generate':
      $url = $module
        ->toUrl('generate-form');
      $form_state
        ->setRedirectUrl($url);
      break;
  }
}