You are here

public function OnlyOneAdminSettings::submitForm in Allow a content type only once (Only One) 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/OnlyOneAdminSettings.php, line 91

Class

OnlyOneAdminSettings
Class OnlyOneAdminSettings.

Namespace

Drupal\onlyone\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Getting the values from the form.
  $onlyone_new_menu_entry_checked = $form_state
    ->getValue('onlyone_new_menu_entry');
  $onlyone_redirect_checked = $form_state
    ->getValue('onlyone_redirect');

  // Getting the onlyone_new_menu_entry variable.
  $onlyone_new_menu_entry = $this
    ->config('onlyone.settings')
    ->get('onlyone_new_menu_entry');
  $onlyone_redirect_entry = $this
    ->config('onlyone.settings')
    ->get('onlyone_redirect');

  // Checking if we have or not changes in the form.
  if ($onlyone_new_menu_entry_checked == $onlyone_new_menu_entry && $onlyone_redirect_checked == $onlyone_redirect_entry) {
    $this
      ->messenger()
      ->addWarning($this
      ->t("You don't have changes in the form."));
  }
  else {
    parent::submitForm($form, $form_state);

    // Saving the module configuration.
    $this
      ->config('onlyone.settings')
      ->set('onlyone_new_menu_entry', $onlyone_new_menu_entry_checked)
      ->save();
    $this
      ->config('onlyone.settings')
      ->set('onlyone_redirect', $onlyone_redirect_checked)
      ->save();

    // If the menu entry has changed we need to rebuild the routes.
    if ($onlyone_new_menu_entry_checked != $onlyone_new_menu_entry) {
      $this->routeBuilder
        ->rebuild();
    }
  }
}