SimplesitemapSettingsForm.php 5.79 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 {

13 14 15 16 17 18 19
  private $form_settings = [
    'max_links',
    'cron_generate',
    'remove_duplicates',
    'skip_untranslated',
    'batch_process_limit'
  ];
gbyte.co's avatar
gbyte.co committed
20

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

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

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

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

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

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

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

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

gbyte.co's avatar
gbyte.co committed
62
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
gbyte.co's avatar
gbyte.co committed
63
      '#type' => 'checkbox',
64 65
      '#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.'),
66
      '#default_value' => $generator->getSetting('cron_generate', TRUE),
gbyte.co's avatar
gbyte.co committed
67
    ];
gbyte.co's avatar
gbyte.co committed
68

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

gbyte.co's avatar
gbyte.co committed
74
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
75
      '#type' => 'checkbox',
76 77
      '#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).'),
78 79 80 81 82 83
      '#default_value' => $generator->getSetting('remove_duplicates', TRUE),
    ];

    $form['simple_sitemap_settings']['advanced']['skip_untranslated'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Skip non-existent translations'),
gbyte.co's avatar
gbyte.co committed
84
      '#description' => $this->t('If checked, only links to the translated content will be included, otherwise the sitemap will include links to all content translation variants, even when the content has not been translated yet.'),
85
      '#default_value' => $generator->getSetting('skip_untranslated', FALSE),
gbyte.co's avatar
gbyte.co committed
86
    ];
gbyte.co's avatar
gbyte.co committed
87

gbyte.co's avatar
gbyte.co committed
88
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
89 90
      '#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."),
91 92 93
      '#type' => 'textfield',
      '#maxlength' => 5,
      '#size' => 5,
94
      '#default_value' => $generator->getSetting('max_links', 2000),
gbyte.co's avatar
gbyte.co committed
95
    ];
96

gbyte.co's avatar
gbyte.co committed
97
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
98 99
      '#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
100 101 102
      '#type' => 'textfield',
      '#maxlength' => 5,
      '#size' => 5,
103
      '#default_value' => $generator->getSetting('batch_process_limit', 1500),
gbyte.co's avatar
gbyte.co committed
104
      '#required' => TRUE,
gbyte.co's avatar
gbyte.co committed
105
    ];
106 107 108 109 110 111 112 113 114 115 116

    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)) {
gbyte.co's avatar
gbyte.co committed
117
        $form_state->setErrorByName('max_links', $this->t("The value of the <em>Maximum links in a sitemap</em> field must be empty, or a positive integer greater than 0."));
118 119
      }
    }
gbyte.co's avatar
gbyte.co committed
120 121 122

  $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)) {
gbyte.co's avatar
gbyte.co committed
123
      $form_state->setErrorByName('batch_process_limit', $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
124
    }
125 126 127 128 129 130
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
131
    $generator = \Drupal::service('simple_sitemap.generator');
gbyte.co's avatar
gbyte.co committed
132 133 134
    foreach($this->form_settings as $setting_name) {
      $generator->saveSetting($setting_name, $form_state->getValue($setting_name));
    }
135
    parent::submitForm($form, $form_state);
136 137
  }

138
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
139
    \Drupal::service('simple_sitemap.generator')->generateSitemap();
140 141
  }
}