SimplesitemapSettingsForm.php 4.75 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

use Drupal\Core\Form\FormStateInterface;

/**
 * SimplesitemapSettingsFrom
 */
gbyte.co's avatar
gbyte.co committed
10
class SimplesitemapSettingsForm extends SimplesitemapFormBase {
11

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

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}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
31

32
    $form['simple_sitemap_settings']['#prefix'] = $this->getDonationText();
33

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

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

gbyte.co's avatar
gbyte.co committed
47
    $form['simple_sitemap_settings']['settings'] = [
48
      '#type' => 'fieldset',
49
      '#title' => $this->t('Settings'),
gbyte.co's avatar
gbyte.co committed
50
    ];
51

gbyte.co's avatar
gbyte.co committed
52
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
gbyte.co's avatar
gbyte.co committed
53
      '#type' => 'checkbox',
54 55
      '#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.'),
56
      '#default_value' => $this->generator->getSetting('cron_generate', TRUE),
gbyte.co's avatar
gbyte.co committed
57
    ];
gbyte.co's avatar
gbyte.co committed
58

gbyte.co's avatar
gbyte.co committed
59
    $form['simple_sitemap_settings']['advanced'] = [
60
      '#type' => 'details',
61
      '#title' => $this->t('Advanced settings'),
62
      '#open' => TRUE,
gbyte.co's avatar
gbyte.co committed
63
    ];
gbyte.co's avatar
gbyte.co committed
64

gbyte.co's avatar
gbyte.co committed
65
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
66
      '#type' => 'checkbox',
67 68
      '#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).'),
69
      '#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
70 71 72 73 74
    ];

    $form['simple_sitemap_settings']['advanced']['skip_untranslated'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Skip non-existent translations'),
gbyte.co's avatar
gbyte.co committed
75
      '#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.'),
76
      '#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
gbyte.co's avatar
gbyte.co committed
77
    ];
gbyte.co's avatar
gbyte.co committed
78

gbyte.co's avatar
gbyte.co committed
79
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
80
      '#type' => 'number',
81
      '#title' => $this->t('Maximum links in a sitemap'),
82
      '#min' => 1,
83
      '#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."),
84
      '#default_value' => $this->generator->getSetting('max_links', 2000),
gbyte.co's avatar
gbyte.co committed
85
    ];
86

gbyte.co's avatar
gbyte.co committed
87
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
88
      '#type' => 'number',
89
      '#title' => $this->t('Refresh batch every n links'),
90
      '#min' => 1,
91
      '#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."),
92
      '#default_value' => $this->generator->getSetting('batch_process_limit', 1500),
gbyte.co's avatar
gbyte.co committed
93
      '#required' => TRUE,
gbyte.co's avatar
gbyte.co committed
94
    ];
95

96
    $this->form->displayRegenerateNow($form['simple_sitemap_settings']);
gbyte.co's avatar
gbyte.co committed
97

98 99 100 101 102 103
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
104
  public function validateForm(array &$form, FormStateInterface $form_state) {}
105 106 107 108 109

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
110
    foreach($this->form_settings as $setting_name) {
111
      $this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
gbyte.co's avatar
gbyte.co committed
112
    }
113
    parent::submitForm($form, $form_state);
gbyte.co's avatar
gbyte.co committed
114 115 116

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
117
      $this->generator->generateSitemap();
gbyte.co's avatar
gbyte.co committed
118
    }
119 120
  }

121
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
122
    $this->generator->generateSitemap();
123 124
  }
}