You are here

public function GoogleImageSitemapDeleteConfirmForm::submitForm in Google Image Sitemap 8

Same name and namespace in other branches
  1. 2.0.x src/Form/GoogleImageSitemapDeleteConfirmForm.php \Drupal\google_image_sitemap\Form\GoogleImageSitemapDeleteConfirmForm::submitForm()
  2. 1.0.x src/Form/GoogleImageSitemapDeleteConfirmForm.php \Drupal\google_image_sitemap\Form\GoogleImageSitemapDeleteConfirmForm::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

src/Form/GoogleImageSitemapDeleteConfirmForm.php, line 76

Class

GoogleImageSitemapDeleteConfirmForm
Provides a confirmation form before clearing out the logs.

Namespace

Drupal\google_image_sitemap\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $query = $this->connection
    ->select('google_image_sitemap', 'gis')
    ->fields('gis', [
    'sid',
    'node_type',
  ])
    ->condition('sid', $this->sitemapId);
  $result = $query
    ->execute()
    ->fetch();
  if (!empty($result)) {
    $this->connection
      ->delete('google_image_sitemap')
      ->condition('sid', $this->sitemapId)
      ->execute();
    $filename = $result->node_type == 'all' ? 'google_image_sitemap.xml' : 'sitemap_' . $result->node_type . '.xml';
    $path = \Drupal::service('file_system')
      ->realpath(file_default_scheme() . "://") . '/google_image_sitemap/' . $filename;
    if (file_exists($path)) {
      file_unmanaged_delete($path);
      drupal_set_message($this
        ->t("Sitemap [@xml_file] deleted successfully!", [
        '@xml_file' => $filename,
      ]));
    }
    else {
      drupal_set_message($this
        ->t("Sitemap deleted successfully!"));
    }
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
    return;
  }
  else {
    throw new NotFoundHttpException();
  }
}