You are here

public function CustomServicesDeleteForm::submitForm in Shorten URLs 8.2

Same name and namespace in other branches
  1. 8 modules/shorten_cs/src/Form/CustomServicesDeleteForm.php \Drupal\shorten_cs\Form\CustomServicesDeleteForm::submitForm()

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 FormInterface::submitForm

File

modules/shorten_cs/src/Form/CustomServicesDeleteForm.php, line 114

Class

CustomServicesDeleteForm
Settings form.

Namespace

Drupal\shorten_cs\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $service = $form_state
    ->getValues()['service'];
  $config_factory = $this
    ->configFactory();
  if ($service == $this
    ->config('shorten.settings')
    ->get('shorten_service')) {
    if ($this
      ->config('shorten.settings')
      ->get('shorten_service_backup')) {
      $config_factory
        ->getEditable('shorten.settings')
        ->set('shorten_service', 'TinyURL')
        ->save();
    }
    else {
      $config_factory
        ->getEditable('shorten.settings')
        ->set('shorten_service', 'is.gd')
        ->save();
    }
    $this
      ->messenger()
      ->addStatus(t('The default URL shortening service was deleted,
                    so it has been reset to @service.', [
      '@service' => \Drupal::config('shorten.settings')
        ->get('shorten_service'),
    ]));
  }
  if ($service == $this
    ->config('shorten.settings')
    ->get('shorten_service_backup')) {
    if ($this
      ->config('shorten.settings')
      ->get('shorten_service')) {
      $config_factory
        ->getEditable('shorten.settings')
        ->set('shorten_service_backup', 'is.gd')
        ->save();
    }
    else {
      $config_factory
        ->getEditable('shorten.settings')
        ->set('shorten_service_backup', 'TinyURL')
        ->save();
    }
    $this
      ->messenger()
      ->addStatus(t('The backup URL shortening service was deleted,
                     so it has been reset to @service.', [
      '@service' => \Drupal::config('shorten.settings')
        ->get('shorten_service_backup'),
    ]));
  }
  $this->database
    ->delete('shorten_cs')
    ->condition('name', $service)
    ->execute();
  $this
    ->messenger()
    ->addStatus(t('The service %service has been deleted.', [
    '%service' => $service,
  ]));
  $form_state
    ->setRedirect('shorten_cs.theme_shorten_cs_admin');
  return;
}