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

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

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

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

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

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

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

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

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

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

70 71 72 73 74 75 76 77
    $form['simple_sitemap_settings']['advanced']['base_url'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Default base URL'),
      '#default_value' => $this->generator->getSetting('base_url', ''),
      '#size' => 30,
      '#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 set here.'),
    ];

gbyte.co's avatar
gbyte.co committed
78
    $form['simple_sitemap_settings']['advanced']['remove_duplicates'] = [
gbyte.co's avatar
gbyte.co committed
79
      '#type' => 'checkbox',
80 81
      '#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).'),
82
      '#default_value' => $this->generator->getSetting('remove_duplicates', TRUE),
83 84 85 86 87
    ];

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

gbyte.co's avatar
gbyte.co committed
92
    $form['simple_sitemap_settings']['advanced']['max_links'] = [
93
      '#type' => 'number',
94
      '#title' => $this->t('Maximum links in a sitemap'),
95
      '#min' => 1,
96
      '#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."),
97
      '#default_value' => $this->generator->getSetting('max_links', 2000),
gbyte.co's avatar
gbyte.co committed
98
    ];
99

gbyte.co's avatar
gbyte.co committed
100
    $form['simple_sitemap_settings']['advanced']['batch_process_limit'] = [
101
      '#type' => 'number',
102
      '#title' => $this->t('Refresh batch every n links'),
103
      '#min' => 1,
104
      '#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."),
105
      '#default_value' => $this->generator->getSetting('batch_process_limit', 1500),
gbyte.co's avatar
gbyte.co committed
106
      '#required' => TRUE,
gbyte.co's avatar
gbyte.co committed
107
    ];
108

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

111 112 113 114 115 116
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
117 118 119 120 121 122 123
  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.'));
    }
  }
124 125 126 127 128

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
gbyte.co's avatar
gbyte.co committed
129
    foreach ($this->formSettings as $setting_name) {
130
      $this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
gbyte.co's avatar
gbyte.co committed
131
    }
132
    parent::submitForm($form, $form_state);
gbyte.co's avatar
gbyte.co committed
133 134 135

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
136
      $this->generator->generateSitemap();
gbyte.co's avatar
gbyte.co committed
137
    }
138 139
  }

gbyte.co's avatar
gbyte.co committed
140 141 142 143
  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
144
  public function generateSitemap(array &$form, FormStateInterface $form_state) {
145
    $this->generator->generateSitemap();
146
  }
gbyte.co's avatar
gbyte.co committed
147

148
}