Commit f0e94fc3 authored by gbyte.co's avatar gbyte.co

Possibility to add custom paths to the sitemap. Added permissions and configuration pages.

parent 4fbfba0f
custom:
home:
-
path: /
index: true
priority: 1
contact:
path: /contact
index: false
priority: 0.5
name: Simple XML Sitemap
type: module
description: 'This module creates a standard conform xml sitemap of your content.'
configure: simplesitemap.settings
package: SEO
core: 8.x
......@@ -5,8 +5,6 @@
* Module install functions creating a database table.
*/
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*/
......@@ -37,7 +35,7 @@ function simplesitemap_schema() {
*/
function simplesitemap_install() {
$base_url = $GLOBALS['base_url'];
drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a> and <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a> pages.)", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy")));
drupal_set_message(t("You can now include content into the <a href='@sitemap_url' target='_blank'>XML sitemap</a> by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url' target='_blank'>content type</a> and <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a> pages.<br/>Custom links can be added on <a href='@config_url' target='_blank'>this configuration page</a>.)", array('@sitemap_url' => "$base_url/sitemap.xml", '@content_type_url' => "$base_url/admin/structure/types", '@taxonomy_vocabulary_url' => "$base_url/admin/structure/taxonomy", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom")));
}
/**
......
# This is the link to GeshiFilter settings in admin/config.
simplesitemap.settings:
title: 'Simple XML sitemap'
description: 'Configure and rebuild the XML sitemap, add custom links to it.'
parent: system.admin_config_search
route_name: simplesitemap.settings
simplesitemap.settings:
route_name: simplesitemap.settings
title: 'Settings'
base_route: simplesitemap.settings
weight: -1
simplesitemap.settings_custom:
route_name: simplesitemap.settings_custom
title: 'Custom links'
base_route: simplesitemap.settings
administer sitemap settings:
title: 'Administer sitemap settings'
description: 'Administer the sitemap settings and generate sitemap on demand.'
restrict access: false
add custom links:
title: 'Add custom links'
description: 'Add any Drupal internal paths to the xml sitemap.'
restrict access: false
......@@ -4,3 +4,19 @@ simplesitemap.sitemap:
_controller: '\Drupal\simplesitemap\Controller\SimplesitemapController::get_sitemap'
requirements:
_permission: 'access content'
simplesitemap.settings:
path: '/admin/config/search/simplesitemap'
defaults:
_form: '\Drupal\simplesitemap\Form\SimplesitemapSettingsForm'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'administer sitemap settings'
simplesitemap.settings_custom:
path: '/admin/config/search/simplesitemap/custom'
defaults:
_form: '\Drupal\simplesitemap\Form\SimplesitemapCustomLinksForm'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'add custom links'
<?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'),
);
$form['simplesitemap_custom']['custom_links'] = array(
'#type' => 'textarea',
'#title' => '<span class="element-invisibule">' . t('Relative Drupal paths') . '</span>',
'#default_value' => $setting_string,
'#prefix' => t('Add custom internal drupal paths to the XML sitemap and specify their priorities.'),
'#description' => t("Please specify drupal internal (relative) paths, one per line. Do not forget to prepend the paths with an '/' You can 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>."),
);
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'));
$custom_links = array_filter(explode("\n", $custom_links_string), 'trim');
$custom_link_config = array();
foreach($custom_links as $link_setting) {
$settings = explode(' ', $link_setting, 2);
$custom_link_config[]['path'] = $settings[0];
if (isset($settings[1])) {
end($custom_link_config);
$key = key($custom_link_config);
$custom_link_config[$key]['priority'] = number_format((float)$settings[1], 1, '.', '');
}
}
$sitemap->save_custom_links($custom_link_config);
$sitemap->generate_all_sitemaps();
parent::submitForm($form, $form_state);
}
}
<?php
/**
* @file
* Contains \Drupal\xmlsitemap\Form\SimplesitemapSettingsForm.
*/
namespace Drupal\simplesitemap\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* SimplesitemapSettingsFrom
*/
class SimplesitemapSettingsForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'simplesitemap_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['simplesitemap.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['simplesitemap_custom'] = array(
'#type' => 'fieldset',
'#title' => t('No settings'),
'#description' => t('There are no settings here yet. Visit the other tab.'),
);
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
......@@ -19,11 +19,11 @@ class CustomLinkGenerator {
public function get_custom_links($custom_paths, $language) {
$links = array();
foreach($custom_paths as $custom_path) {
if ($custom_path['index']) {
if (!isset($custom_path['index']) || $custom_path['index']) {
$links[] = SitemapGenerator::add_xml_link_markup(Url::fromUserInput($custom_path['path'], array(
'language' => $language,
'absolute' => TRUE
))->toString(), $custom_path['priority']);
))->toString(), isset($custom_path['priority']) ? $custom_path['priority'] : SitemapGenerator::PRIORITY_DEFAULT);
}
}
return $links;
......
......@@ -72,6 +72,10 @@ class Simplesitemap {
$this->save_config('entity_types', $entity_types);
}
public function save_custom_links($custom_links) {
$this->save_config('custom', $custom_links);
}
private function save_config($key, $value) {
\Drupal::service('config.factory')->getEditable('simplesitemap.settings')->set($key, $value)->save();
$this->set_config();
......@@ -140,4 +144,8 @@ class Simplesitemap {
public function get_entity_types() {
return $this->config->get('entity_types');
}
public function get_custom_links() {
return $this->config->get('custom');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment