SimplesitemapSettingsForm.php 5.01 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
 * @package Drupal\simple_sitemap\Form
10
 */
gbyte.co's avatar
gbyte.co committed
11
class SimplesitemapSettingsForm extends SimplesitemapFormBase {
12

13
  private $formSettings = [
14 15 16 17 18 19
    '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}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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