...
 
Commits (4)
simplesitemap.settings:
type: config_object
mapping:
settings:
type: mapping
mapping:
max_links:
label: 'Max Links'
type: integer
cron_generate:
label: 'Cron Generate'
type: boolean
custom:
label: 'Custom'
type: sequence
sequence:
type: mapping
mapping:
path:
label: 'Path'
type: string
priority:
label: 'Priority'
type: float
entity_types:
label: 'Entity Types'
type: sequence
sequence:
type: sequence
sequence:
type: mapping
mapping:
index:
label: 'Index'
type: integer
priority:
label: 'Priority'
type: float
...@@ -102,6 +102,8 @@ function simplesitemap_entity_form_submit($form, &$form_state) { ...@@ -102,6 +102,8 @@ function simplesitemap_entity_form_submit($form, &$form_state) {
// Regenerate sitemaps according to user setting. // Regenerate sitemaps according to user setting.
if ($values['simplesitemap_regenerate_now']) { if ($values['simplesitemap_regenerate_now']) {
$sitemap->generate_sitemap(); $sitemap->generate_sitemap();
drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.",
array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
} }
} }
......
...@@ -29,6 +29,9 @@ class SimplesitemapController { ...@@ -29,6 +29,9 @@ class SimplesitemapController {
$output = $sitemap->get_sitemap($sitemap_id); $output = $sitemap->get_sitemap($sitemap_id);
// Display sitemap with correct xml header. // Display sitemap with correct xml header.
return new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml')); $response = new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
$meta_data = $response->getCacheableMetadata();
$meta_data->addCacheTags(['simplesitemap']);
return $response;
} }
} }
...@@ -119,6 +119,8 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase { ...@@ -119,6 +119,8 @@ class SimplesitemapCustomLinksForm extends ConfigFormBase {
// Regenerate sitemaps according to user setting. // Regenerate sitemaps according to user setting.
if ($form_state->getValue('simplesitemap_regenerate_now')) { if ($form_state->getValue('simplesitemap_regenerate_now')) {
$sitemap->generate_sitemap(); $sitemap->generate_sitemap();
drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.",
array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
} }
} }
} }
...@@ -100,5 +100,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase { ...@@ -100,5 +100,7 @@ class SimplesitemapSettingsForm extends ConfigFormBase {
public function generate_sitemap(array &$form, FormStateInterface $form_state) { public function generate_sitemap(array &$form, FormStateInterface $form_state) {
$sitemap = new Simplesitemap; $sitemap = new Simplesitemap;
$sitemap->generate_sitemap(); $sitemap->generate_sitemap();
drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.",
array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
} }
} }
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
namespace Drupal\simplesitemap; namespace Drupal\simplesitemap;
use Drupal\Core\Cache\Cache;
/** /**
* Simplesitemap class. * Simplesitemap class.
...@@ -171,13 +172,12 @@ class Simplesitemap { ...@@ -171,13 +172,12 @@ class Simplesitemap {
* Generates the sitemap for all languages and saves it to the db. * Generates the sitemap for all languages and saves it to the db.
*/ */
public function generate_sitemap() { public function generate_sitemap() {
Cache::invalidateTags(array('simplesitemap'));
$generator = new SitemapGenerator(); $generator = new SitemapGenerator();
$generator->set_custom_links($this->get_config('custom')); $generator->set_custom_links($this->get_config('custom'));
$generator->set_entity_types($this->get_config('entity_types')); $generator->set_entity_types($this->get_config('entity_types'));
$this->sitemap = $generator->generate_sitemap($this->get_setting('max_links')); $this->sitemap = $generator->generate_sitemap($this->get_setting('max_links'));
$this->save_sitemap(); $this->save_sitemap();
drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.",
array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
} }
/** /**
......
<?php
/**
* @file
* Contains \Drupal\simplesitemap\Tests\SimplesitemapTest
*/
namespace Drupal\simplesitemap\Tests;
use Drupal\simplesitemap\Simplesitemap;
use Drupal\simpletest\WebTestBase;
/**
* Tests Simple XML sitemap integration.
*
* @group Simplesitemap
*/
class SimplesitemapTest extends WebTestBase {
protected $dumpHeaders = TRUE;
protected $strictConfigSchema = FALSE;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['simplesitemap', 'node'];
/**
* Implements setup().
*/
protected function setUp() {
parent::setUp();
$this->drupalCreateContentType(['type' => 'page']);
$this->config('simplesitemap.settings')
->set('entity_types', ['node_type' => ['page' => ['index' => 1, 'priority' => '0.5']]])
->save();
}
/**
* Test Simple sitemap integration.
*/
public function testSimplesitemap() {
$sitemap = new Simplesitemap;
$sitemap->generate_sitemap();
// Verify sitemap.xml can be cached.
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
/* @var $node \Drupal\Node\NodeInterface */
$node = $this->createNode(['title' => 'A new page']);
// Generate new sitemap.
$sitemap->generate_sitemap();
// Verify the cache was flushed and node is in the sitemap.
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->assertText('node/' . $node->id());
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this->assertText('node/' . $node->id());
}
}