Commit 2ae93cb0 authored by gbyte.co's avatar gbyte.co

Clean up SitemapGenerator class

parent acdc063d
......@@ -78,28 +78,15 @@ class Batch {
}
/**
* Adds operations to the batch of type 'entity_types' or 'custom_paths'.
* Adds an operation to the batch.
*
* @param string $type
* @param array $operations
* @param string $processing_method
* @param array $data
*/
public function addOperations($type, $operations) {
switch ($type) {
case 'entity_types':
foreach ($operations as $operation) {
$this->batch['operations'][] = [
__CLASS__ . '::generateBundleUrls',
[$operation['entity_info'], $this->batchInfo]
];
};
break;
case 'custom_paths':
$this->batch['operations'][] = [
__CLASS__ . '::generateCustomUrls',
[$operations, $this->batchInfo]
];
break;
}
public function addOperation($processing_method, $data) {
$this->batch['operations'][] = [
__CLASS__ . '::' . $processing_method, [$data, $this->batchInfo]
];
}
/**
......@@ -260,13 +247,13 @@ class Batch {
self::SetCurrentId($i, $context);
}
$user_input = $custom_path['path'][0] === '/' ? $custom_path['path'] : '/' . $custom_path['path'];
// $user_input = $custom_path['path'][0] === '/' ? $custom_path['path'] : '/' . $custom_path['path']; // Not needed due to checks on form submit
if (!\Drupal::service('path.validator')->isValid($custom_path['path'])) { //todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush.
self::registerError(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, ['@faulty_path' => $custom_path['path']], 'warning');
continue;
}
$options = ['absolute' => TRUE, 'language' => $languages[Simplesitemap::getDefaultLangId()]];
$url_object = Url::fromUserInput($user_input, $options);
$url_object = Url::fromUserInput($custom_path['path'], $options);
if (!$url_object->access($batch_info['anonymous_user_account']))
continue;
......
......@@ -47,29 +47,35 @@ class SitemapGenerator {
'remove_duplicates' => $this->generator->getSetting('remove_duplicates'),
'entity_types' => $this->generator->getConfig('entity_types'),
]);
$batch->addOperations('custom_paths', $this->batchAddCustomPaths());
$batch->addOperations('entity_types', $this->batchAddEntityTypePaths());
// Add custom link generating operation.
$batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());
// Add entity link generating operations.
foreach($this->getEntityTypeData() as $data) {
$batch->addOperation('generateBundleUrls', $data);
}
$batch->start();
}
/**
* Returns the custom path generating operation.
* Returns a batch-ready data array for custom link generation.
*
* @return array $operation.
* @return array $data
* Data to be processed.
*/
private function batchAddCustomPaths() {
private function getCustomUrlsData() {
$link_generator = new CustomLinkGenerator();
return $link_generator->getCustomPaths($this->generator->getConfig('custom'));
}
/**
* Collects entity metadata for entities that are set to be indexed
* and returns a batch-ready operation.
* and returns an array of batch-ready data sets for entity link generation.
*
* @return array $operations.
*/
private function batchAddEntityTypePaths() {
$operations = [];
private function getEntityTypeData() {
$data_sets = [];
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
$entity_types = $this->generator->getConfig('entity_types');
foreach($entity_types as $entity_type_name => $bundles) {
......@@ -78,19 +84,17 @@ class SitemapGenerator {
$keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix.
foreach($bundles as $bundle_name => $bundle_settings) {
if ($bundle_settings['index']) {
$operations[] = [
'entity_info' => [
'bundle_settings' => $bundle_settings,
'bundle_name' => $bundle_name,
'entity_type_name' => $entity_type_name,
'keys' => $keys,
],
$data_sets[] = [
'bundle_settings' => $bundle_settings,
'bundle_name' => $bundle_name,
'entity_type_name' => $entity_type_name,
'keys' => $keys,
];
}
}
}
}
return $operations;
return $data_sets;
}
/**
......@@ -104,7 +108,7 @@ class SitemapGenerator {
*/
public static function generateSitemap($links, $remove_sitemap = FALSE) {
// Invoke alter hook.
\Drupal::moduleHandler()->alter('simple_sitemap_links', $links);
\Drupal::moduleHandler()->alter('simple_sitemap_links', $links);
$values = [
'id' => $remove_sitemap ? 1 : \Drupal::service('database')->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
'sitemap_string' => self::generateSitemapChunk($links),
......
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