SimplesitemapCustomLinksForm.php 4.43 KB
Newer Older
1 2
<?php

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

use Drupal\Core\Form\FormStateInterface;

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

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
Pawel G's avatar
Pawel G committed
17
    return 'simple_sitemap_custom_links_form';
18 19 20 21 22 23 24 25
  }

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

    $setting_string = '';
26
    foreach ($this->generator->getConfig('custom') as $custom_link) {
27
      $setting_string .= isset($custom_link['priority'])
28
        ? $custom_link['path'] . ' ' . $this->formHelper->formatPriority($custom_link['priority'])
29
        : $custom_link['path'];
30 31 32
      $setting_string .= "\r\n";
    }

Pawel G's avatar
Pawel G committed
33
    $form['simple_sitemap_custom'] = [
34
      '#title' => $this->t('Custom links'),
35
      '#type' => 'fieldset',
36
      '#markup' => '<p>' . $this->t('Add custom internal drupal paths to the XML sitemap.') . '</p>',
37
      '#prefix' => $this->getDonationText(),
Pawel G's avatar
Pawel G committed
38
    ];
39

Pawel G's avatar
Pawel G committed
40
    $form['simple_sitemap_custom']['custom_links'] = [
41
      '#type' => 'textarea',
42
      '#title' => $this->t('Relative Drupal paths'),
43
      '#default_value' => $setting_string,
44
      '#description' => $this->t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with a '/'. You can optionally add a priority (0.0 - 1.0) by appending it to the path after a space. The home page with the highest priority would be <em>/ 1.0</em>, the contact page with the default priority would be <em>/contact 0.5</em>."),
Pawel G's avatar
Pawel G committed
45
    ];
46

47
    $this->formHelper->displayRegenerateNow($form['simple_sitemap_custom']);
48

49 50 51 52 53 54 55
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
Pawel G's avatar
Pawel G committed
56 57
    foreach($this->getCustomLinks($form_state->getValue('custom_links')) as $i => $link_config) {
      $placeholders = ['@line' => ++$i, '@path' => $link_config['path'], '@priority' => isset($link_config['priority']) ? $link_config['priority'] : ''];
58

Pawel G's avatar
Pawel G committed
59
      // Checking if internal path exists.
60
      if (!$this->pathValidator->isValid($link_config['path'])
Pawel G's avatar
Pawel G committed
61 62
       || strpos($link_config['path'], '//') !== FALSE) { // Path validator does not see a double slash as an error. Catching this to prevent breaking path generation.
        $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> does not exist.", $placeholders));
63
      }
Pawel G's avatar
Pawel G committed
64 65

      // Making sure the paths start with a slash.
66
      if ($link_config['path'][0] != '/') {
Pawel G's avatar
Pawel G committed
67
        $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> needs to start with a '/'.", $placeholders));
68
      }
Pawel G's avatar
Pawel G committed
69 70

      // Making sure the priority is formatted correctly.
71
      if (isset($link_config['priority']) && !FormHelper::isValidPriority($link_config['priority'])) {
Pawel G's avatar
Pawel G committed
72
        $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The priority setting <em>@priority</em> for path <em>@path</em> is incorrect. Set the priority from 0.0 to 1.0.", $placeholders));
73 74 75 76 77 78 79 80
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
Pawel G's avatar
Pawel G committed
81
    $custom_links = $this->getCustomLinks($form_state->getValue('custom_links'));
82
    $this->generator->removeCustomLinks();
Pawel G's avatar
Pawel G committed
83
    foreach ($custom_links as $link_config) {
84
      $this->generator->addCustomLink($link_config['path'], $link_config);
85
    }
86 87
    parent::submitForm($form, $form_state);

88
    // Regenerate sitemaps according to user setting.
Pawel G's avatar
Pawel G committed
89
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
90
      $this->generator->generateSitemap();
91
    }
92
  }
93

Pawel G's avatar
Pawel G committed
94 95 96 97
  /**
   * @param $custom_links_string
   * @return array
   */
Pawel G's avatar
Pawel G committed
98
  private function getCustomLinks($custom_links_string) {
99 100
    // Unify newline characters and explode into array.
    $custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));
101
    // Remove empty values and whitespaces from array.
102
    $custom_links_string_lines = array_filter(array_map('trim', $custom_links_string_lines));
Pawel G's avatar
Pawel G committed
103
    $custom_links = [];
104 105
    foreach($custom_links_string_lines as $i => &$line) {
      $link_settings = explode(' ', $line, 2);
Pawel G's avatar
Pawel G committed
106
      $custom_links[$i]['path'] = $link_settings[0];
107
      if (isset($link_settings[1]) && $link_settings[1] != '') {
Pawel G's avatar
Pawel G committed
108
        $custom_links[$i]['priority'] = $link_settings[1];
109 110
      }
    }
Pawel G's avatar
Pawel G committed
111
    return $custom_links;
112
  }
113
}