Commit 09b9ea0f authored by gbyte.co's avatar gbyte.co

Clean up a few classes, prevent simple_sitemap service from loading xml...

Clean up a few classes, prevent simple_sitemap service from loading xml sitemap chunks from DB on every initialization
parent 4f85f41c
......@@ -35,14 +35,12 @@ function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
if (isset($form['additional_settings'])) {
$form['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
}
$f->displayEntitySitemapSettings($form['simple_sitemap']);
$f->displaySitemapRegenerationSetting($form['simple_sitemap']);
}
else {
return;
}
$form['simple_sitemap']['#simple_sitemap'] = $f;
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
......@@ -70,7 +68,7 @@ function simple_sitemap_entity_form_submit($form, &$form_state) {
// Only make changes in DB if sitemap settings actually changed.
if (Form::valuesChanged($form, $values)) {
$sitemap = \Drupal::service('simple_sitemap.generator');
$f = $form['simple_sitemap']['#simple_sitemap'];
$f = new Form($form_state);
switch ($f->entityCategory) {
......
......@@ -14,19 +14,18 @@ use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
class Batch {
private $batch;
private $batchInfo;
const PATH_DOES_NOT_EXIST = "The path @faulty_path has been omitted from the XML sitemap, as it does not exist.";
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS = "The path @faulty_path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users.";
const ANONYMOUS_USER_ID = 0;
const BATCH_INIT_MESSAGE = 'Initializing batch...';
const BATCH_ERROR_MESSAGE = 'An error has occurred. This may result in an incomplete XML sitemap.';
const BATCH_PROGRESS_MESSAGE = 'Processing @current out of @total link types.';
const ANONYMOUS_USER_ID = 0;
function __construct($from = 'form') {
function __construct() {
$this->batch = array(
'title' => t('Generating XML sitemap'),
'init_message' => t(self::BATCH_INIT_MESSAGE),
......@@ -35,15 +34,11 @@ class Batch {
'operations' => array(),
'finished' => [__CLASS__ , 'finishGeneration'], // __CLASS__ . '::finishGeneration' not working possibly due to a drush error.
);
$config = \Drupal::config('simple_sitemap.settings')->get('settings');
$this->batchInfo = array(
'from' => $from,
'batch_process_limit' => !empty($config['batch_process_limit']) ? $config['batch_process_limit'] : NULL,
'max_links' => $config['max_links'],
'remove_duplicates' => $config['remove_duplicates'],
'entity_types' => \Drupal::config('simple_sitemap.settings')->get('entity_types'),
'anonymous_user_account' => User::load(self::ANONYMOUS_USER_ID),
);
}
public function setBatchInfo($batch_info) {
$this->batchInfo = $batch_info;
$this->batchInfo['anonymous_user_account'] = User::load(self::ANONYMOUS_USER_ID);
}
/**
......
......@@ -301,10 +301,9 @@ class Simplesitemap {
* This decides how the batch process is to be run.
*/
public function generateSitemap($from = 'form') {
$generator = new SitemapGenerator($from);
$generator->setCustomLinks($this->getConfig('custom'));
$generator->setEntityTypes($this->getConfig('entity_types'));
$generator->startBatch();
$generator = new SitemapGenerator($this);
$generator->setGenerateFrom($from);
$generator->startGeneration();
}
/**
......@@ -317,7 +316,7 @@ class Simplesitemap {
* The sitemap index.
*/
private function getSitemapIndex($sitemap) {
$generator = new SitemapGenerator();
$generator = new SitemapGenerator($this);
return $generator->generateSitemapIndex($sitemap);
}
......
......@@ -20,29 +20,32 @@ class SitemapGenerator {
const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
private $entityTypes;
private $custom;
private $sitemap;
private $links;
private $generatingFrom;
function __construct($from = 'form') {
function __construct($sitemap) {
$this->sitemap = $sitemap;
$this->links = array();
$this->generatingFrom = $from;
}
public function setEntityTypes($entityTypes) {
$this->entityTypes = is_array($entityTypes) ? $entityTypes : array();
}
public function setCustomLinks($custom) {
$this->custom = is_array($custom) ? $custom : array();
public function setGenerateFrom($from = 'form') {
$this->generatingFrom = $from;
}
/**
* Adds all operations to the batch and starts it.
*/
public function startBatch() {
$batch = new Batch($this->generatingFrom);
public function startGeneration() {
$batch = new Batch();
$batch->setBatchInfo([
'from' => $this->generatingFrom,
'batch_process_limit' => !empty($this->sitemap->getSetting('batch_process_limit'))
? $this->sitemap->getSetting('batch_process_limit') : NULL,
'max_links' => $this->sitemap->getSetting('max_links'),
'remove_duplicates' => $this->sitemap->getSetting('remove_duplicates'),
'entity_types' => $this->sitemap->getConfig('entity_types'),
]);
$batch->addOperations('custom_paths', $this->batchAddCustomPaths());
$batch->addOperations('entity_types', $this->batchAddEntityTypePaths());
$batch->start();
......@@ -55,7 +58,7 @@ class SitemapGenerator {
*/
private function batchAddCustomPaths() {
$link_generator = new CustomLinkGenerator();
return $link_generator->getCustomPaths($this->custom);
return $link_generator->getCustomPaths($this->sitemap->getConfig('custom'));
}
/**
......@@ -67,7 +70,8 @@ class SitemapGenerator {
private function batchAddEntityTypePaths() {
$operations = [];
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
foreach($this->entityTypes as $entity_type_name => $bundles) {
$entity_types = $this->sitemap->getConfig('entity_types');
foreach($entity_types as $entity_type_name => $bundles) {
if (isset($sitemap_entity_types[$entity_type_name])) {
$keys = $sitemap_entity_types[$entity_type_name]->getKeys();
$keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix.
......
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