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

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

use Drupal\Core\Form\FormStateInterface;
6
use Drupal\Component\Utility\UrlHelper;
7 8

/**
gbyte.co's avatar
gbyte.co committed
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 15 16

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

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

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

gbyte.co's avatar
gbyte.co committed
28
    $form['simple_sitemap_settings']['regenerate'] = [
29
      '#type' => 'fieldset',
30
      '#title' => $this->t('Regenerate sitemap'),
31
      '#markup' => '<p>' . $this->t('This will regenerate the XML sitemap immediately.') . '</p>',
gbyte.co's avatar
gbyte.co committed
32
    ];
33

gbyte.co's avatar
gbyte.co committed
34
    $form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
35
      '#type' => 'submit',
36
      '#value' => $this->t('Regenerate sitemap'),
gbyte.co's avatar
gbyte.co committed
37
      '#submit' => ['::generateSitemap'],
gbyte.co's avatar
gbyte.co committed
38
      // Skip form-level validator.
gbyte.co's avatar
gbyte.co committed
39
      '#validate' => [],
gbyte.co's avatar
gbyte.co committed
40
    ];
41

gbyte.co's avatar
gbyte.co committed
42
    $form['simple_sitemap_settings']['settings'] = [
43
      '#type' => 'fieldset',
44
      '#title' => $this->t('Settings'),
gbyte.co's avatar
gbyte.co committed
45
    ];
46

gbyte.co's avatar
gbyte.co committed
47
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
gbyte.co's avatar
gbyte.co committed
48
      '#type' => 'checkbox',
49
      '#title' => $this->t('Regenerate the sitemap during cron runs'),
50
      '#description' => $this->t('Uncheck this if you intend to only regenerate the sitemap manually or via drush.'),
51
      '#default_value' => $this->generator->getSetting('cron_generate', TRUE),
gbyte.co's avatar
gbyte.co committed
52
    ];
gbyte.co's avatar
gbyte.co committed
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    $form['simple_sitemap_settings']['settings']['cron_generate_interval'] = [
      '#type' => 'select',
      '#title' => $this->t('Sitemap generation interval'),
      '#description' => $this->t('The sitemap will be generated according to this interval.'),
      '#default_value' => $this->generator->getSetting('cron_generate_interval', 0),
      '#options' => [
        0 => $this->t('On every cron run'),
        1 => $this->t('Once an hour'),
        3 => $this->t('Once every @hours hours', ['@hours' => 3]),
        6 => $this->t('Once every @hours hours', ['@hours' => 6]),
        12 => $this->t('Once every @hours hours', ['@hours' => 12]),
        24 => $this->t('Once a day'),
        48 => $this->t('Once every @days days', ['@days' => 48/24]),
        72 => $this->t('Once every @days days', ['@days' => 72/24]),
        96 => $this->t('Once every @days days', ['@days' => 96/24]),
        120 => $this->t('Once every @days days', ['@days' => 120/24]),
        144 => $this->t('Once every @days days', ['@days' => 144/24]),
        168 => $this->t('Once a week'),
      ],
      '#states' => [
        'visible' => [
          ':input[name="cron_generate"]' => ['checked' => TRUE],
        ],
      ],
    ];

gbyte.co's avatar
gbyte.co committed
80
    $form['simple_sitemap_settings']['advanced'] = [
81
      '#type' => 'details',
82
      '#title' => $this->t('Advanced settings'),
83
      '#open' => TRUE,
gbyte.co's avatar
gbyte.co committed
84
    ];
gbyte.co's avatar
gbyte.co committed
85

86 87 88 89 90
    $form['simple_sitemap_settings']['advanced']['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Default base URL'),
      '#default_value' => $this->generator->getSetting('base_url', ''),
      '#size' => 30,
91
      '#description' => $this->t('On some hosting providers it is impossible to pass parameters to cron to tell Drupal which URL to bootstrap with. In this case the base URL of sitemap links can be overridden here.<br/>Example: <em>@url</em>', ['@url' => $GLOBALS['base_url']]),
92 93
    ];

gbyte.co's avatar
gbyte.co committed
94
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
95
      '#type' => 'checkbox',
96 97
      '#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).'),
98
      '#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
99 100
    ];

gbyte.co's avatar
gbyte.co committed
101
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
102
      '#type' => 'number',
103
      '#title' => $this->t('Maximum links in a sitemap'),
104
      '#min' => 1,
gbyte.co's avatar
gbyte.co committed
105
      '#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.'),
106
      '#default_value' => $this->generator->getSetting('max_links', 2000),
gbyte.co's avatar
gbyte.co committed
107
    ];
108

gbyte.co's avatar
gbyte.co committed
109
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
110
      '#type' => 'number',
111
      '#title' => $this->t('Refresh batch every n links'),
112
      '#min' => 1,
gbyte.co's avatar
gbyte.co committed
113
      '#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.'),
114
      '#default_value' => $this->generator->getSetting('batch_process_limit', 1500),
gbyte.co's avatar
gbyte.co committed
115
      '#required' => TRUE,
gbyte.co's avatar
gbyte.co committed
116
    ];
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    $form['simple_sitemap_settings']['advanced']['languages'] = [
      '#type' => 'details',
      '#title' => $this->t('Language settings'),
      '#open' => FALSE,
    ];

    $language_options = [];
    foreach ($this->languageManager->getLanguages() as $language) {
      if (!$language->isDefault()) {
        $language_options[$language->getId()] = $language->getName();
      }
    }

    $form['simple_sitemap_settings']['advanced']['languages']['skip_untranslated'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Skip non-existent translations'),
      '#description' => $this->t('If checked, entity links are generated exclusively for languages the entity has been translated to as long as the language is not excluded below.<br/>Otherwise entity links are generated for every language installed on the site apart from languages excluded below.<br/>This setting has no effect on non-entity paths like homepage.'),
      '#default_value' => $this->generator->getSetting('skip_untranslated', FALSE),
    ];

    $form['simple_sitemap_settings']['advanced']['languages']['excluded_languages'] = [
      '#title' => $this->t('Exclude languages'),
      '#type' => 'checkboxes',
      '#options' => $language_options,
      '#description' => $this->t('There will be no links generated for languages checked here.'),
      '#default_value' => $this->generator->getSetting('excluded_languages', []),
    ];

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

148 149 150 151 152 153
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
154 155 156
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $base_url = $form_state->getValue('base_url');
    $form_state->setValue('base_url', rtrim($base_url, '/'));
gbyte.co's avatar
gbyte.co committed
157
    if ($base_url !== '' && !UrlHelper::isValid($base_url, TRUE)) {
158 159 160
      $form_state->setErrorByName('base_url', t('The base URL is invalid.'));
    }
  }
161 162 163 164 165

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
166 167 168 169 170 171 172
    foreach (['max_links',
               'cron_generate',
               'cron_generate_interval',
               'remove_duplicates',
               'skip_untranslated',
               'batch_process_limit',
               'base_url',] as $setting_name) {
173
      $this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
gbyte.co's avatar
gbyte.co committed
174
    }
175 176
    $this->generator->saveSetting('excluded_languages', array_filter($form_state->getValue('excluded_languages')));

177
    parent::submitForm($form, $form_state);
gbyte.co's avatar
gbyte.co committed
178 179 180

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
181
      $this->generator->generateSitemap();
gbyte.co's avatar
gbyte.co committed
182
    }
183 184
  }

gbyte.co's avatar
gbyte.co committed
185 186 187 188
  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
189
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
190
    $this->generator->generateSitemap('nobatch');
191
  }
gbyte.co's avatar
gbyte.co committed
192

193
}