SimplesitemapSettingsForm.php 5.88 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 20 21
    'max_links',
    'cron_generate',
    'remove_duplicates',
    'skip_untranslated',
gbyte.co's avatar
gbyte.co committed
22
    'batch_process_limit',
23
    'base_url',
24
  ];
gbyte.co's avatar
gbyte.co committed
25

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

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

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

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

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

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

gbyte.co's avatar
gbyte.co committed
59
    $form['simple_sitemap_settings']['settings']['cron_generate'] = [
gbyte.co's avatar
gbyte.co committed
60
      '#type' => 'checkbox',
61 62
      '#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.'),
63
      '#default_value' => $this->generator->getSetting('cron_generate', 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'] = [
67
      '#type' => 'details',
68
      '#title' => $this->t('Advanced settings'),
69
      '#open' => TRUE,
gbyte.co's avatar
gbyte.co committed
70
    ];
gbyte.co's avatar
gbyte.co committed
71

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

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

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

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

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

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

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

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

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

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

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

150
}