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

gbyte.co's avatar
gbyte.co committed
3
namespace Drupal\simple_sitemap\Form;
4 5 6 7

use Drupal\Core\Form\FormStateInterface;

/**
gbyte.co's avatar
gbyte.co committed
8 9
 * Class SimplesitemapSettingsForm.
 *
gbyte.co's avatar
gbyte.co committed
10
 * @package Drupal\simple_sitemap\Form
11
 */
gbyte.co's avatar
gbyte.co committed
12
class SimplesitemapSettingsForm extends SimplesitemapFormBase {
13

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

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

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

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

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

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

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

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

gbyte.co's avatar
gbyte.co committed
62
    $form['simple_sitemap_settings']['advanced'] = [
63
      '#type' => 'details',
64
      '#title' => $this->t('Advanced settings'),
65
      '#open' => TRUE,
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).'),
72
      '#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
73 74 75 76 77
    ];

    $form['simple_sitemap_settings']['advanced']['skip_untranslated'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Skip non-existent translations'),
78
      '#description' => $this->t('If unchecked, entity links are generated for every language installed on the site, regardless whether the entity has been translated to a language or not.<br/>If checked, entity links are generated exclusively for languages the entity has been translated to. This setting has no effect on non-entity paths like homepage.'),
79
      '#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
gbyte.co's avatar
gbyte.co committed
80
    ];
gbyte.co's avatar
gbyte.co committed
81

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

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

99
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_settings']);
gbyte.co's avatar
gbyte.co committed
100

101 102 103 104 105 106
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
107
  public function validateForm(array &$form, FormStateInterface $form_state) {}
108 109 110 111 112

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

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

gbyte.co's avatar
gbyte.co committed
124 125 126 127
  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
128
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
129
    $this->generator->generateSitemap();
130
  }
gbyte.co's avatar
gbyte.co committed
131

132
}