SimplesitemapCustomLinksForm.php 4.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php

/**
 * @file
 * Contains \Drupal\xmlsitemap\Form\SimplesitemapCustomLinksForm.
 */

namespace Drupal\simplesitemap\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\simplesitemap\Simplesitemap;

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

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'simplesitemap_custom_links_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['simplesitemap.settings_custom'];
  }

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

    $sitemap = new Simplesitemap;
    $setting_string = '';
    foreach ($sitemap->get_custom_links() as $custom_link) {

      // todo: remove this statement after removing the index key from the configuration.
      if (isset($custom_link['index']) && $custom_link['index'] == 0)
        continue;

      $setting_string .= isset($custom_link['priority']) ? $custom_link['path'] . ' ' . $custom_link['priority'] : $custom_link['path'];
      $setting_string .= "\r\n";
    }

    $form['simplesitemap_custom'] = array(
      '#title' => t('Custom links'),
52 53
      '#type' => 'fieldset',
      '#markup' => '<p>' . t('Add custom internal drupal paths and their priorities to the XML sitemap.') . '</p>',
54 55 56 57
    );

    $form['simplesitemap_custom']['custom_links'] = array(
      '#type' => 'textarea',
58
      '#title' => t('Relative Drupal paths'),
59
      '#default_value' => $setting_string,
60
      '#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</em>, the contact page with a medium priority would be <em>/contact 0.5</em>."),
61
    );
62 63 64 65 66 67 68 69

    $form['simplesitemap_custom']['simplesitemap_rebuild_now'] = array(
      '#type' => 'checkbox',
      '#title' => t('Rebuild sitemap after hitting Save'),
      '#description' => t('This setting will rebuild the whole sitemap including the above changes.<br/>Otherwise the sitemap will be rebuilt on next cron run.'),
      '#default_value' => FALSE,
    );

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $custom_links_string = str_replace("\r\n", "\n", $form_state->getValue('custom_links'));
    $custom_links = array_filter(explode("\n", $custom_links_string), 'trim');

    foreach($custom_links as $link_setting) {
      $settings = explode(' ', $link_setting, 2);

      if (!\Drupal::service('path.validator')->isValid($settings[0])) {
        $form_state->setErrorByName('', t("The path <em>$settings[0]</em> does not exist."));
      }
      if ($settings[0][0] != '/') {
        $form_state->setErrorByName('', t("The path <em>$settings[0]</em> needs to start with an '/'."));
      }
      if (isset($settings[1])) {
        if (!is_numeric($settings[1]) || $settings[1] < 0 || $settings[1] > 1) {
          $form_state->setErrorByName('', t("Priority setting on line <em>$link_setting</em> is incorrect. Set priority from 0.0 to 1.0."));
        }
      }
    }
  }

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

    $sitemap = new Simplesitemap;
    $custom_links_string = str_replace("\r\n", "\n", $form_state->getValue('custom_links'));
104
    $custom_links_string_lines = array_filter(explode("\n", $custom_links_string), 'trim');
105
    $custom_link_config = array();
106 107 108 109
    foreach($custom_links_string_lines as $line) {
      $line_settings = explode(' ', $line, 2);
      $custom_link_config[]['path'] = $line_settings[0];
      if (isset($line_settings[1])) {
110 111
        end($custom_link_config);
        $key = key($custom_link_config);
112
        $custom_link_config[$key]['priority'] = number_format((float)$line_settings[1], 1, '.', '');
113 114 115
      }
    }
    $sitemap->save_custom_links($custom_link_config);
116 117 118

    // Regenerate sitemaps according to user setting.
    if ($form_state->getValue('simplesitemap_rebuild_now')) {
119
      $sitemap->generate_sitemap();
120
    }
121 122
  }
}