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

Pawel G's avatar
Pawel G committed
3
namespace Drupal\simple_sitemap\Form;
4 5

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

/**
Pawel G's avatar
Pawel G committed
9
 * Class SimplesitemapSettingsForm
Pawel G's avatar
Pawel G committed
10
 * @package Drupal\simple_sitemap\Form
11
 */
Pawel G's avatar
Pawel G committed
12
class SimplesitemapSettingsForm extends SimplesitemapFormBase {
13 14 15 16

  /**
   * {@inheritdoc}
   */
Pawel G's avatar
Pawel G committed
17
  public function getFormId() {
Pawel G's avatar
Pawel G 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

Pawel G's avatar
Pawel G 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>',
Pawel G's avatar
Pawel G committed
32
    ];
33

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

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

Pawel G's avatar
Pawel G committed
47
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
Pawel G's avatar
Pawel G 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),
Pawel G's avatar
Pawel G committed
52
    ];
Pawel G's avatar
Pawel G 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],
        ],
      ],
    ];

Pawel G's avatar
Pawel G committed
80
    $form['simple_sitemap_settings']['advanced'] = [
81
      '#type' => 'details',
82
      '#title' => $this->t('Advanced settings'),
83
      '#open' => TRUE,
Pawel G's avatar
Pawel G committed
84
    ];
Pawel G's avatar
Pawel G 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
    ];

Pawel G's avatar
Pawel G committed
94
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
Pawel G's avatar
Pawel G 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
    ];

Pawel G's avatar
Pawel G committed
101
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
102
      '#type' => 'number',
103
      '#title' => $this->t('Maximum links in a sitemap'),
104
      '#min' => 1,
Pawel G's avatar
Pawel G 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),
Pawel G's avatar
Pawel G committed
107
    ];
108

Pawel G's avatar
Pawel G 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,
Pawel G's avatar
Pawel G 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),
Pawel G's avatar
Pawel G committed
115
      '#required' => TRUE,
Pawel G's avatar
Pawel G committed
116
    ];
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    $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'),
Pawel G's avatar
Pawel G committed
134
      '#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/>Bear in mind that non-entity paths like homepage will always be generated for every non-excluded language.'),
135 136 137 138 139 140 141
      '#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,
Pawel G's avatar
Pawel G committed
142 143 144
      '#description' => !empty($language_options)
        ? $this->t('There will be no links generated for languages checked here.')
        : $this->t('There are no languages other than the default language <a href="@url">available</a>.', ['@url' => $GLOBALS['base_url'] . '/admin/config/regional/language']),
145 146 147
      '#default_value' => $this->generator->getSetting('excluded_languages', []),
    ];

148
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_settings']);
Pawel G's avatar
Pawel G committed
149

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

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

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

179
    parent::submitForm($form, $form_state);
Pawel G's avatar
Pawel G committed
180 181 182

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
183
      $this->generator->generateSitemap();
Pawel G's avatar
Pawel G committed
184
    }
185 186
  }

Pawel G's avatar
Pawel G committed
187 188 189 190
  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
191
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
Pawel G's avatar
Pawel G committed
192
    $this->generator->generateSitemap();
193
  }
Pawel G's avatar
Pawel G committed
194

195
}