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

gbyte.co's avatar
gbyte.co committed
14 15 16
  /**
   * @var array
   */
17
  protected $formSettings = [
18 19
    'max_links',
    'cron_generate',
20
    'cron_generate_interval',
21 22
    'remove_duplicates',
    'skip_untranslated',
gbyte.co's avatar
gbyte.co committed
23
    'batch_process_limit',
24
    'base_url',
25
  ];
gbyte.co's avatar
gbyte.co committed
26

27 28 29 30
  /**
   * {@inheritdoc}
   */
  public function getFormID() {
gbyte.co's avatar
gbyte.co committed
31
    return 'simple_sitemap_settings_form';
32 33 34 35 36 37
  }

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

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

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

gbyte.co's avatar
gbyte.co committed
47
    $form['simple_sitemap_settings']['regenerate']['regenerate_submit'] = [
48
      '#type' => 'submit',
49
      '#value' => $this->t('Regenerate sitemap'),
gbyte.co's avatar
gbyte.co committed
50
      '#submit' => ['::generateSitemap'],
gbyte.co's avatar
gbyte.co committed
51 52
    // Skip form-level validator.
      '#validate' => [],
gbyte.co's avatar
gbyte.co committed
53
    ];
54

gbyte.co's avatar
gbyte.co committed
55
    $form['simple_sitemap_settings']['settings'] = [
56
      '#type' => 'fieldset',
57
      '#title' => $this->t('Settings'),
gbyte.co's avatar
gbyte.co committed
58
    ];
59

gbyte.co's avatar
gbyte.co committed
60
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
gbyte.co's avatar
gbyte.co committed
61
      '#type' => 'checkbox',
62
      '#title' => $this->t('Regenerate the sitemap during cron runs'),
63
      '#description' => $this->t('Uncheck this if you intend to only regenerate the sitemap manually or via drush.'),
64
      '#default_value' => $this->generator->getSetting('cron_generate', TRUE),
gbyte.co's avatar
gbyte.co committed
65
    ];
gbyte.co's avatar
gbyte.co committed
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    $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
93
    $form['simple_sitemap_settings']['advanced'] = [
94
      '#type' => 'details',
95
      '#title' => $this->t('Advanced settings'),
96
      '#open' => TRUE,
gbyte.co's avatar
gbyte.co committed
97
    ];
gbyte.co's avatar
gbyte.co committed
98

99 100 101 102 103
    $form['simple_sitemap_settings']['advanced']['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Default base URL'),
      '#default_value' => $this->generator->getSetting('base_url', ''),
      '#size' => 30,
104
      '#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']]),
105 106
    ];

gbyte.co's avatar
gbyte.co committed
107
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
108
      '#type' => 'checkbox',
109 110
      '#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).'),
111
      '#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
112 113 114 115 116
    ];

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

gbyte.co's avatar
gbyte.co committed
121
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
122
      '#type' => 'number',
123
      '#title' => $this->t('Maximum links in a sitemap'),
124
      '#min' => 1,
125
      '#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."),
126
      '#default_value' => $this->generator->getSetting('max_links', 2000),
gbyte.co's avatar
gbyte.co committed
127
    ];
128

gbyte.co's avatar
gbyte.co committed
129
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
130
      '#type' => 'number',
131
      '#title' => $this->t('Refresh batch every n links'),
132
      '#min' => 1,
133
      '#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."),
134
      '#default_value' => $this->generator->getSetting('batch_process_limit', 1500),
gbyte.co's avatar
gbyte.co committed
135
      '#required' => TRUE,
gbyte.co's avatar
gbyte.co committed
136
    ];
137

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

140 141 142 143 144 145
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
146 147 148 149 150 151 152
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $base_url = $form_state->getValue('base_url');
    $form_state->setValue('base_url', rtrim($base_url, '/'));
    if ($base_url != '' && !UrlHelper::isValid($base_url, TRUE)) {
      $form_state->setErrorByName('base_url', t('The base URL is invalid.'));
    }
  }
153 154 155 156 157

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
158
    foreach ($this->formSettings as $setting_name) {
159
      $this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
gbyte.co's avatar
gbyte.co committed
160
    }
161
    parent::submitForm($form, $form_state);
gbyte.co's avatar
gbyte.co committed
162 163 164

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
165
      $this->generator->generateSitemap();
gbyte.co's avatar
gbyte.co committed
166
    }
167 168
  }

gbyte.co's avatar
gbyte.co committed
169 170 171 172
  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
173
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
174
    $this->generator->generateSitemap();
175
  }
gbyte.co's avatar
gbyte.co committed
176

177
}