Interim commit.
[yaffs-website] / web / modules / contrib / simple_sitemap / src / Form / SimplesitemapCustomLinksForm.php
1 <?php
2
3 namespace Drupal\simple_sitemap\Form;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Class SimplesitemapCustomLinksForm
9  * @package Drupal\simple_sitemap\Form
10  */
11 class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function getFormID() {
17     return 'simple_sitemap_custom_links_form';
18   }
19
20   /**
21    * {@inheritdoc}
22    */
23   public function buildForm(array $form, FormStateInterface $form_state) {
24
25     $form['simple_sitemap_custom'] = [
26       '#title' => $this->t('Custom links'),
27       '#type' => 'fieldset',
28       '#markup' => '<p>' . $this->t('Add custom internal drupal paths to the XML sitemap.') . '</p>',
29       '#prefix' => $this->getDonationText(),
30     ];
31
32     $form['simple_sitemap_custom']['custom_links'] = [
33       '#type' => 'textarea',
34       '#title' => $this->t('Relative Drupal paths'),
35       '#default_value' => $this->customLinksToString($this->generator->getCustomLinks()),
36       '#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>."),
37     ];
38
39     $this->formHelper->displayRegenerateNow($form['simple_sitemap_custom']);
40
41     return parent::buildForm($form, $form_state);
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function validateForm(array &$form, FormStateInterface $form_state) {
48     foreach ($this->stringToCustomLinks($form_state->getValue('custom_links')) as $i => $link_config) {
49       $placeholders = ['@line' => ++$i, '@path' => $link_config['path'], '@priority' => isset($link_config['priority']) ? $link_config['priority'] : ''];
50
51       // Checking if internal path exists.
52       if (!$this->pathValidator->isValid($link_config['path'])
53 //      if (!$this->pathValidator->getUrlIfValidWithoutAccessCheck($link_config['path']) //todo
54       // Path validator does not see a double slash as an error. Catching this to prevent breaking path generation.
55        || strpos($link_config['path'], '//') !== FALSE) {
56         $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> does not exist.", $placeholders));
57       }
58
59       // Making sure the paths start with a slash.
60       if ($link_config['path'][0] != '/') {
61         $form_state->setErrorByName('', $this->t("<strong>Line @line</strong>: The path <em>@path</em> needs to start with a '/'.", $placeholders));
62       }
63
64       // Making sure the priority is formatted correctly.
65       if (isset($link_config['priority']) && !FormHelper::isValidPriority($link_config['priority'])) {
66         $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));
67       }
68     }
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function submitForm(array &$form, FormStateInterface $form_state) {
75     $custom_links = $this->stringToCustomLinks($form_state->getValue('custom_links'));
76     $this->generator->removeCustomLinks();
77     foreach ($custom_links as $link_config) {
78       $this->generator->addCustomLink($link_config['path'], $link_config);
79     }
80     parent::submitForm($form, $form_state);
81
82     // Regenerate sitemaps according to user setting.
83     if ($form_state->getValue('simple_sitemap_regenerate_now')) {
84       $this->generator->generateSitemap();
85     }
86   }
87
88   /**
89    * @param $custom_links_string
90    * @return array
91    */
92   protected function stringToCustomLinks($custom_links_string) {
93
94     // Unify newline characters and explode into array.
95     $custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));
96
97     // Remove empty values and whitespaces from array.
98     $custom_links_string_lines = array_filter(array_map('trim', $custom_links_string_lines));
99
100     $custom_links = [];
101     foreach ($custom_links_string_lines as $i => &$line) {
102       $link_settings = explode(' ', $line, 2);
103       $custom_links[$i]['path'] = $link_settings[0];
104       if (isset($link_settings[1]) && $link_settings[1] != '') {
105         $custom_links[$i]['priority'] = $link_settings[1];
106       }
107     }
108     return $custom_links;
109   }
110
111   /**
112    * @param array $links
113    * @return string
114    */
115   protected function customLinksToString(array $links) {
116     $setting_string = '';
117     foreach ($links as $custom_link) {
118       $setting_string .= isset($custom_link['priority'])
119         ? $custom_link['path'] . ' ' . $this->formHelper->formatPriority($custom_link['priority'])
120         : $custom_link['path'];
121       $setting_string .= "\r\n";
122     }
123     return $setting_string;
124   }
125 }