SimplesitemapCustomLinksForm.php 4.27 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
gbyte.co's avatar
gbyte.co committed
5
 * Contains \Drupal\simple_sitemap\Form\SimplesitemapCustomLinksForm.
6 7
 */

gbyte.co's avatar
gbyte.co committed
8
namespace Drupal\simple_sitemap\Form;
9 10 11

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\simple_sitemap\Form;
13 14 15 16 17 18 19 20 21 22

/**
 * SimplesitemapCustomLinksFrom
 */
class SimplesitemapCustomLinksForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
gbyte.co's avatar
gbyte.co committed
23
    return 'simple_sitemap_custom_links_form';
24 25 26 27 28 29
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
gbyte.co's avatar
gbyte.co committed
30
    return ['simple_sitemap.settings_custom'];
31 32 33 34 35 36 37
  }

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

38
    $generator = \Drupal::service('simple_sitemap.generator');
39
    $setting_string = '';
40
    foreach ($generator->getConfig('custom') as $custom_link) {
41 42 43
      $setting_string .= isset($custom_link['priority'])
        ? $custom_link['path'] . ' ' . Form::formatPriority($custom_link['priority'])
        : $custom_link['path'];
44 45 46
      $setting_string .= "\r\n";
    }

47
    $form['simple_sitemap_custom'] = [
48
      '#title' => t('Custom links'),
49
      '#type' => 'fieldset',
50
      '#markup' => '<p>' . t('Add custom internal drupal paths to the XML sitemap.') . '</p>',
51
    ];
52

53
    $form['simple_sitemap_custom']['custom_links'] = [
54
      '#type' => 'textarea',
55
      '#title' => t('Relative Drupal paths'),
56
      '#default_value' => $setting_string,
57
      '#description' => 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>."),
58
    ];
59

60 61
    $f = new Form();
    $f->displaySitemapRegenerationSetting($form['simple_sitemap_custom']);
62

63 64 65 66 67 68 69 70
    return parent::buildForm($form, $form_state);
  }

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

71
    $custom_link_config = $this->getCustomLinkConfig($form_state->getValue('custom_links'));
72

73 74 75
    foreach($custom_link_config as $link_config) {

      if (!\Drupal::service('path.validator')->isValid($link_config['path'])) {
76
        $form_state->setErrorByName('', t("The path <em>@path</em> does not exist.", ['@path' => $link_config['path']]));
77
      }
78
      if ($link_config['path'][0] != '/') {
79
        $form_state->setErrorByName('', t("The path <em>@path</em> needs to start with a '/'.", ['@path' => $link_config['path']]));
80
      }
81
      if (isset($link_config['priority'])) {
82
        if (!Form::isValidPriority($link_config['priority'])) {
83
          $form_state->setErrorByName('', t("The priority setting <em>@priority</em> for path <em>@path</em> is incorrect. Set the priority from 0.0 to 1.0.", ['@priority' => $link_config['priority'], '@path' => $link_config['path']]));
84 85 86 87 88 89 90 91 92
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
93
    $generator = \Drupal::service('simple_sitemap.generator');
94
    $custom_link_config = $this->getCustomLinkConfig($form_state->getValue('custom_links'));
95
    $generator->removeCustomLinks();
96
    foreach ($custom_link_config as $link_config) {
97
      $generator->addCustomLink($link_config['path'], $link_config);
98
    }
99 100
    parent::submitForm($form, $form_state);

101
    // Regenerate sitemaps according to user setting.
gbyte.co's avatar
gbyte.co committed
102
    if ($form_state->getValue('simple_sitemap_regenerate_now')) {
103
      $generator->generateSitemap();
104
    }
105
  }
106

107 108 109 110 111
  private function getCustomLinkConfig($custom_links_string) {
    // Unify newline characters and explode into array.
    $custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));
    // Remove whitespace from array values.
    $custom_links_string_lines = array_filter(array_map('trim', $custom_links_string_lines));
112
    $custom_link_config = [];
113 114 115 116 117
    foreach($custom_links_string_lines as $i => &$line) {
      $link_settings = explode(' ', $line, 2);
      $custom_link_config[$i]['path'] = $link_settings[0];
      if (isset($link_settings[1]) && $link_settings[1] != '') {
        $custom_link_config[$i]['priority'] = $link_settings[1];
118 119 120 121
      }
    }
    return $custom_link_config;
  }
122
}