SimplesitemapSettingsForm.php 5.21 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
gbyte.co's avatar
gbyte.co committed
5
 * Contains \Drupal\simple_sitemap\Form\SimplesitemapSettingsForm.
6 7
 */

gbyte.co's avatar
gbyte.co committed
8
namespace Drupal\simple_sitemap\Form;
9 10 11 12 13 14 15 16 17

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * SimplesitemapSettingsFrom
 */
class SimplesitemapSettingsForm extends ConfigFormBase {

gbyte.co's avatar
gbyte.co committed
18 19
  private $form_settings = ['max_links', 'cron_generate', 'remove_duplicates', 'batch_process_limit'];

20 21 22 23
  /**
   * {@inheritdoc}
   */
  public function getFormID() {
gbyte.co's avatar
gbyte.co committed
24
    return 'simple_sitemap_settings_form';
25 26 27 28 29 30
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
gbyte.co's avatar
gbyte.co committed
31
    return ['simple_sitemap.settings'];
32 33 34 35 36 37
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
38

gbyte.co's avatar
gbyte.co committed
39
    $generator = \Drupal::service('simple_sitemap.generator');
40

gbyte.co's avatar
gbyte.co committed
41
    $form['simple_sitemap_settings']['#prefix'] = "<div class='description'>" .t("If you would like to say thanks and support the development of this module, a <a target='_blank' href='@url'>donation</a> is always appreciated.", ['@url' => 'https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5AFYRSBLGSC3W']) . "</div>";
42

gbyte.co's avatar
gbyte.co committed
43
    $form['simple_sitemap_settings']['regenerate'] = [
44
      '#title' => t('Regenerate sitemap'),
45
      '#type' => 'fieldset',
46
      '#markup' => '<p>' . t('This will regenerate the XML sitemap for all languages.') . '</p>',
gbyte.co's avatar
gbyte.co committed
47
    ];
48

gbyte.co's avatar
gbyte.co committed
49
    $form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
50
      '#type' => 'submit',
51
      '#value' => t('Regenerate sitemap'),
gbyte.co's avatar
gbyte.co committed
52 53 54
      '#submit' => ['::generateSitemap'],
      '#validate' => [], // Skip form-level validator.
    ];
55

gbyte.co's avatar
gbyte.co committed
56
    $form['simple_sitemap_settings']['settings'] = [
gbyte.co's avatar
gbyte.co committed
57
      '#title' => t('Settings'),
58
      '#type' => 'fieldset',
gbyte.co's avatar
gbyte.co committed
59
    ];
60

gbyte.co's avatar
gbyte.co committed
61
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
gbyte.co's avatar
gbyte.co committed
62 63 64
      '#type' => 'checkbox',
      '#title' => t('Regenerate the sitemap on every cron run'),
      '#description' => t('Uncheck this if you intend to only regenerate the sitemap manually or via drush.'),
gbyte.co's avatar
gbyte.co committed
65
      '#default_value' => $generator->getSetting('cron_generate'),
gbyte.co's avatar
gbyte.co committed
66
    ];
gbyte.co's avatar
gbyte.co committed
67

gbyte.co's avatar
gbyte.co committed
68
    $form['simple_sitemap_settings']['advanced'] = [
gbyte.co's avatar
gbyte.co committed
69 70
      '#title' => t('Advanced settings'),
      '#type' => 'details',
gbyte.co's avatar
gbyte.co committed
71
    ];
gbyte.co's avatar
gbyte.co committed
72

gbyte.co's avatar
gbyte.co committed
73
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
74
      '#type' => 'checkbox',
75
      '#title' => t('Exclude duplicate links'),
76
      '#description' => t('Uncheck this to significantly speed up the sitemap generation process on a huge site (more than 20 000 indexed entities).'),
gbyte.co's avatar
gbyte.co committed
77
      '#default_value' => $generator->getSetting('remove_duplicates'),
gbyte.co's avatar
gbyte.co committed
78
    ];
gbyte.co's avatar
gbyte.co committed
79

gbyte.co's avatar
gbyte.co committed
80
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
81 82 83 84 85
      '#title' => t('Maximum links in a sitemap'),
      '#description' => t("The maximum number of links one sitemap can hold. If more links are generated than set here, a sitemap index will be created and the links split into several sub-sitemaps.<br/>50 000 links is the maximum Google will parse per sitemap, however it is advisable to set this to a lower number. If left blank, all links will be shown on a single sitemap."),
      '#type' => 'textfield',
      '#maxlength' => 5,
      '#size' => 5,
gbyte.co's avatar
gbyte.co committed
86
      '#default_value' => $generator->getSetting('max_links'),
gbyte.co's avatar
gbyte.co committed
87
    ];
88

gbyte.co's avatar
gbyte.co committed
89
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
gbyte.co's avatar
gbyte.co committed
90
      '#title' => t('Refresh batch every n links'),
91
      '#description' => t("During sitemap generation, the batch process will issue a page refresh after n links processed to prevent PHP timeouts and memory exhaustion. Increasing this number will reduce the number of times Drupal has to bootstrap (thus speeding up the generation process), but will require more memory and less strict PHP timeout settings."),
gbyte.co's avatar
gbyte.co committed
92 93 94
      '#type' => 'textfield',
      '#maxlength' => 5,
      '#size' => 5,
gbyte.co's avatar
gbyte.co committed
95
      '#default_value' => $generator->getSetting('batch_process_limit'),
gbyte.co's avatar
gbyte.co committed
96
    ];
97 98 99 100 101 102 103 104 105 106 107

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $max_links = $form_state->getValue('max_links');
    if ($max_links != '') {
      if (!is_numeric($max_links) || $max_links < 1 || $max_links != round($max_links)) {
108
        $form_state->setErrorByName('', t("The value of the <em>Maximum links in a sitemap</em> field must be empty, or a positive integer greater than 0."));
109 110
      }
    }
gbyte.co's avatar
gbyte.co committed
111 112 113

  $batch_process_limit = $form_state->getValue('batch_process_limit');
    if (!is_numeric($batch_process_limit) || $batch_process_limit < 1 || $batch_process_limit != round($batch_process_limit)) {
114
      $form_state->setErrorByName('', t("The value of the <em>Refresh batch every n links</em> field must be a positive integer greater than 0."));
gbyte.co's avatar
gbyte.co committed
115
    }
116 117 118 119 120 121
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
122
    $generator = \Drupal::service('simple_sitemap.generator');
gbyte.co's avatar
gbyte.co committed
123 124 125
    foreach($this->form_settings as $setting_name) {
      $generator->saveSetting($setting_name, $form_state->getValue($setting_name));
    }
126
    parent::submitForm($form, $form_state);
127 128
  }

129
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
130 131
    $generator = \Drupal::service('simple_sitemap.generator');
    $generator->generateSitemap();
132 133
  }
}