SimplesitemapSettingsForm.php 5.26 KB
Newer Older
1 2
<?php

gbyte.co's avatar
gbyte.co committed
3
namespace Drupal\simple_sitemap\Form;
4 5 6 7 8 9 10 11 12

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

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

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

15 16 17 18
  /**
   * {@inheritdoc}
   */
  public function getFormID() {
gbyte.co's avatar
gbyte.co committed
19
    return 'simple_sitemap_settings_form';
20 21 22 23 24 25
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
gbyte.co's avatar
gbyte.co committed
26
    return ['simple_sitemap.settings'];
27 28 29 30 31 32
  }

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

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

gbyte.co's avatar
gbyte.co committed
36
    $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>";
37

gbyte.co's avatar
gbyte.co committed
38
    $form['simple_sitemap_settings']['regenerate'] = [
39
      '#title' => $this->t('Regenerate sitemap'),
40
      '#type' => 'fieldset',
41
      '#markup' => '<p>' . $this->t('This will regenerate the XML sitemap for all languages.') . '</p>',
gbyte.co's avatar
gbyte.co committed
42
    ];
43

gbyte.co's avatar
gbyte.co committed
44
    $form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
45
      '#type' => 'submit',
46
      '#value' => $this->t('Regenerate sitemap'),
gbyte.co's avatar
gbyte.co committed
47 48 49
      '#submit' => ['::generateSitemap'],
      '#validate' => [], // Skip form-level validator.
    ];
50

gbyte.co's avatar
gbyte.co committed
51
    $form['simple_sitemap_settings']['settings'] = [
52
      '#title' => $this->t('Settings'),
53
      '#type' => 'fieldset',
gbyte.co's avatar
gbyte.co committed
54
    ];
55

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

gbyte.co's avatar
gbyte.co committed
63
    $form['simple_sitemap_settings']['advanced'] = [
64
      '#title' => $this->t('Advanced settings'),
gbyte.co's avatar
gbyte.co committed
65
      '#type' => 'details',
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']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
69
      '#type' => 'checkbox',
70 71
      '#title' => $this->t('Exclude duplicate links'),
      '#description' => $this->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
72
      '#default_value' => $generator->getSetting('remove_duplicates'),
gbyte.co's avatar
gbyte.co committed
73
    ];
gbyte.co's avatar
gbyte.co committed
74

gbyte.co's avatar
gbyte.co committed
75
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
76 77
      '#title' => $this->t('Maximum links in a sitemap'),
      '#description' => $this->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."),
78 79 80
      '#type' => 'textfield',
      '#maxlength' => 5,
      '#size' => 5,
gbyte.co's avatar
gbyte.co committed
81
      '#default_value' => $generator->getSetting('max_links'),
gbyte.co's avatar
gbyte.co committed
82
    ];
83

gbyte.co's avatar
gbyte.co committed
84
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
85 86
      '#title' => $this->t('Refresh batch every n links'),
      '#description' => $this->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
87 88 89
      '#type' => 'textfield',
      '#maxlength' => 5,
      '#size' => 5,
gbyte.co's avatar
gbyte.co committed
90
      '#default_value' => $generator->getSetting('batch_process_limit'),
91
      '#required' => TRUE, //TODO: test
gbyte.co's avatar
gbyte.co committed
92
    ];
93 94 95 96 97 98 99 100 101 102 103

    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)) {
104
        $form_state->setErrorByName('', $this->t("The value of the <em>Maximum links in a sitemap</em> field must be empty, or a positive integer greater than 0."));
105 106
      }
    }
gbyte.co's avatar
gbyte.co committed
107 108 109

  $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)) {
110
      $form_state->setErrorByName('', $this->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
111
    }
112 113 114 115 116 117
  }

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

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