3 namespace Drupal\simple_sitemap\Form;
5 use Drupal\Core\Form\FormStateInterface;
8 * Class SimplesitemapCustomLinksForm
9 * @package Drupal\simple_sitemap\Form
11 class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
16 public function getFormID() {
17 return 'simple_sitemap_custom_links_form';
23 public function buildForm(array $form, FormStateInterface $form_state) {
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(),
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>."),
39 $this->formHelper->displayRegenerateNow($form['simple_sitemap_custom']);
41 return parent::buildForm($form, $form_state);
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'] : ''];
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));
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));
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));
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);
80 parent::submitForm($form, $form_state);
82 // Regenerate sitemaps according to user setting.
83 if ($form_state->getValue('simple_sitemap_regenerate_now')) {
84 $this->generator->generateSitemap();
89 * @param $custom_links_string
92 protected function stringToCustomLinks($custom_links_string) {
94 // Unify newline characters and explode into array.
95 $custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));
97 // Remove empty values and whitespaces from array.
98 $custom_links_string_lines = array_filter(array_map('trim', $custom_links_string_lines));
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];
108 return $custom_links;
112 * @param array $links
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";
123 return $setting_string;