SimplesitemapCustomLinksForm.php 4.15 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
    $sitemap = \Drupal::service('simple_sitemap.generator');
39
    $setting_string = '';
40
    foreach ($sitemap->getConfig('custom') as $custom_link) {
41 42 43 44
      $setting_string .= isset($custom_link['priority']) ? $custom_link['path'] . ' ' . $custom_link['priority'] : $custom_link['path'];
      $setting_string .= "\r\n";
    }

gbyte.co's avatar
gbyte.co committed
45
    $form['simple_sitemap_custom'] = array(
46
      '#title' => t('Custom links'),
47
      '#type' => 'fieldset',
48
      '#markup' => '<p>' . t('Add custom internal drupal paths to the XML sitemap.') . '</p>',
49 50
    );

gbyte.co's avatar
gbyte.co committed
51
    $form['simple_sitemap_custom']['custom_links'] = array(
52
      '#type' => 'textarea',
53
      '#title' => t('Relative Drupal paths'),
54
      '#default_value' => $setting_string,
55
      '#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>."),
56
    );
57

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

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

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

69
    $custom_link_config = $this->getCustomLinks($form_state->getValue('custom_links'));
70

71 72 73 74
    foreach($custom_link_config as $link_config) {

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
91
    $sitemap = \Drupal::service('simple_sitemap.generator');
92
    $custom_link_config = $this->getCustomLinks($form_state->getValue('custom_links'));
93 94 95
    foreach($custom_link_config as &$link_config) {
      if (isset($link_config['priority'])) {
        $link_config['priority'] = number_format((float)$link_config['priority'], 1, '.', '');
96 97
      }
    }
98
    $sitemap->saveConfig('custom', $custom_link_config);
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
      $sitemap->generateSitemap();
104
    }
105
  }
106

107
  private function getCustomLinks($custom_links_string) {
108 109 110 111 112 113 114 115 116 117 118
    $custom_links_string_lines = array_filter(explode("\n", str_replace("\r\n", "\n", $custom_links_string)), 'trim');
    $custom_link_config = array();
    foreach($custom_links_string_lines as $i => $line) {
      $line_settings = explode(' ', $line, 2);
      $custom_link_config[$i]['path'] = $line_settings[0];
      if (isset($line_settings[1])) {
        $custom_link_config[$i]['priority'] = $line_settings[1];
      }
    }
    return $custom_link_config;
  }
119
}