Commit 24c16c9b authored by gbyte.co's avatar gbyte.co

V2 initial commit.

parent 4aac9619
......@@ -8,3 +8,5 @@ entity_types: []
settings:
max_links: 2000
cron_generate: true
remove_duplicates: true
batch_process_limit: 1500
/**
* @file
* Attaches simplesitemap behaviors to the entity form.
* Attaches simple_sitemap behaviors to the entity form.
*/
(function($) {
"use strict";
Drupal.behaviors.simplesitemapFieldsetSummaries = {
Drupal.behaviors.simple_sitemapFieldsetSummaries = {
attach: function(context) {
$(context).find('#edit-simplesitemap').drupalSetSummary(function (context) {
$(context).find('#edit-simple-sitemap').drupalSetSummary(function (context) {
var vals = [];
if ($(context).find('#edit-simplesitemap-index-content').is(':checked')) {
if ($(context).find('#edit-simple-sitemap-index-content').is(':checked')) {
// Display summary of the settings in tabs.
vals.push(Drupal.t('Included in sitemap'));
vals.push(Drupal.t('Priority') + ' ' + $('#edit-simplesitemap-priority option:selected', context).text());
vals.push(Drupal.t('Priority') + ' ' + $('#edit-simple-sitemap-priority option:selected', context).text());
}
else {
// Display summary of the settings in tabs.
......
/**
* @file
* Attaches simplesitemap behaviors to the entity form.
* Attaches simple_sitemap behaviors to the entity form.
*
* @todo: Tidy up.
*/
......@@ -9,33 +9,33 @@
"use strict";
// Hide the 'Regenerate sitemap' field to only display it if settings have changed.
$('.form-item-simplesitemap-regenerate-now').hide();
$('.form-item-simple-sitemap-regenerate-now').hide();
Drupal.behaviors.simplesitemapForm = {
Drupal.behaviors.simple_sitemapForm = {
attach: function(context) {
if ($(context).find('#edit-simplesitemap-index-content').is(':checked')) {
if ($(context).find('#edit-simple-sitemap-index-content').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simplesitemap-priority').show();
$('.form-item-simple-sitemap-priority').show();
}
else { // Hide 'Priority' field if 'Index sitemap' is unticked.
$('.form-item-simplesitemap-priority').hide();
$('.form-item-simple-sitemap-priority').hide();
}
// Show 'Regenerate sitemap' field if setting has changed.
$( "#edit-simplesitemap-index-content" ).change(function() {
$('.form-item-simplesitemap-regenerate-now').show();
if ($(context).find('#edit-simplesitemap-index-content').is(':checked')) {
$( "#edit-simple-sitemap-index-content" ).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
if ($(context).find('#edit-simple-sitemap-index-content').is(':checked')) {
// Show 'Priority' field if 'Index sitemap' is ticked.
$('.form-item-simplesitemap-priority').show();
$('.form-item-simple-sitemap-priority').show();
}
else { // Hide 'Priority' field if 'Index sitemap' is unticked.
$('.form-item-simplesitemap-priority').hide();
$('.form-item-simple-sitemap-priority').hide();
}
});
// Show 'Regenerate sitemap' field if setting has changed.
$( "#edit-simplesitemap-priority" ).change(function() {
$('.form-item-simplesitemap-regenerate-now').show();
$( "#edit-simple-sitemap-priority" ).change(function() {
$('.form-item-simple-sitemap-regenerate-now').show();
});
}
};
......
......@@ -4,16 +4,16 @@
* Drush integration.
*/
use Drupal\simplesitemap\Simplesitemap;
use Drupal\simple_sitemap\Simplesitemap;
/**
* Implements hook_drush_command().
*/
function simplesitemap_drush_command() {
$items['simplesitemap-generate'] = array(
function simple_sitemap_drush_command() {
$items['simple_sitemap-generate'] = array(
'description' => 'Regenerate XML sitemaps for all languages according to the module settings.',
'callback' => 'drush_simplesitemap_generate',
'drupal dependencies' => array('simplesitemap'),
'callback' => 'drush_simple_sitemap_generate',
'drupal dependencies' => array('simple_sitemap'),
);
return $items;
}
......@@ -23,7 +23,7 @@ function simplesitemap_drush_command() {
*
* Regenerate sitemap for all languages.
*/
function drush_simplesitemap_generate() {
function drush_simple_sitemap_generate() {
$sitemap = new Simplesitemap;
$sitemap->generate_sitemap();
$sitemap->generate_sitemap('drush');
}
name: Simple XML Sitemap
type: module
description: 'This module creates a standard conform xml sitemap of your content.'
configure: simplesitemap.settings
configure: simple_sitemap.settings
package: SEO
core: 8.x
......@@ -4,14 +4,16 @@
* Module install and update procedures.
*/
use Drupal\simple_sitemap\Simplesitemap;
/**
* Implements hook_requirements().
*/
function simplesitemap_requirements($phase) {
function simple_sitemap_requirements($phase) {
$requirements = array();
if (!extension_loaded('xmlwriter')) {
$requirements['simplesitemap_php_extensions'] = array(
$requirements['simple_sitemap_php_extensions'] = array(
'title' => t('Simple XML sitemap PHP extensions'),
'value' => t('Missing PHP xmlwriter extension'),
'description' => t("In order to be able to generate sitemaps, the Simple XML sitemap module requires the <em>xmlwriter</em> PHP extension to be enabled."),
......@@ -21,19 +23,19 @@ function simplesitemap_requirements($phase) {
switch($phase) {
case 'runtime':
$sitemap = new \Drupal\simplesitemap\Simplesitemap();
$sitemap = new \Drupal\simple_sitemap\Simplesitemap();
$generated_ago = $sitemap->get_generated_ago();
if (!$generated_ago) {
$value = t('Not available');
$description = t("The sitemap has not been generated yet. Run cron, or <a href='@generate'>generate</a> the sitemap manually.", array('@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
$description = t("The sitemap has not been generated yet. Run cron, or <a href='@generate'>generate</a> the sitemap manually.", array('@generate' => $GLOBALS['base_url'] . '/admin/config/search/simple_sitemap'));
$severity = REQUIREMENT_WARNING;
}
else {
$value = t('XML sitemap is available');
$description = t("The <a href='@sitemap'>XML sitemap</a> was generated @ago ago. <a href='@generate'>Regenerate</a>.", array('@ago' => $generated_ago, '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml', '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'));
$description = t("The <a href='@sitemap'>XML sitemap</a> was generated @ago ago. <a href='@generate'>Regenerate</a>.", array('@ago' => $generated_ago, '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml', '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simple_sitemap'));
$severity = REQUIREMENT_INFO;
}
$requirements['simplesitemap_generated'] = array(
$requirements['simple_sitemap_generated'] = array(
'title' => 'Simple XML sitemap',
'value' => $value,
'description' => $description,
......@@ -47,8 +49,8 @@ function simplesitemap_requirements($phase) {
/**
* Implements hook_schema().
*/
function simplesitemap_schema() {
$schema['simplesitemap'] = array(
function simple_sitemap_schema() {
$schema['simple_sitemap'] = array(
'description' => 'Holds XML sitemaps as strings for quick retrieval.',
'fields' => array(
'id' => array(
......@@ -78,93 +80,10 @@ function simplesitemap_schema() {
/**
* Implements hook_install().
*/
function simplesitemap_install() {
function simple_sitemap_install() {
$sitemap = new Simplesitemap;
$sitemap->generate_sitemap('drush');
$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>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</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", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simplesitemap/custom")));
}
/**
* Update: Altering the {simplesitemap} language_code table field to hold longer strings.
*/
function simplesitemap_update_8101() {
db_change_field('simplesitemap', 'language_code', 'language_code', array(
'type' => 'varchar',
'not null' => TRUE,
'length' => 12,));
}
/**
* Changing config settings according to new config structure.
*/
function simplesitemap_update_8102() {
$entity_types = \Drupal::config('simplesitemap.settings')->get('entity_types');
$entity_types = is_array($entity_types) ? $entity_types : array();
if (isset($entity_types['node'])) {
$entity_types['node_type'] = $entity_types['node'];
unset($entity_types['node']);
}
if (isset($entity_types['taxonomy_term'])) {
$entity_types['taxonomy_vocabulary'] = $entity_types['taxonomy_term'];
unset($entity_types['taxonomy_term']);
}
\Drupal::service('config.factory')->getEditable('simplesitemap.settings')
->set('entity_types', $entity_types)->save();
}
/**
* Modifying database structure to hold one multilingual sitemap.
*/
function simplesitemap_update_8103() {
db_truncate('simplesitemap')->execute();
db_drop_primary_key('simplesitemap');
db_change_field('simplesitemap', 'language_code', 'id', array(
'type' => 'serial',
'not null' => TRUE
), array('primary key' => array('id')));
return t('Before the sitemap can be accessed, it must be regenerated manually or via cron run.');
}
/**
* Modifying database structure to accommodate sitemap chunks.
*/
function simplesitemap_update_8104() {
db_truncate('simplesitemap')->execute();
db_change_field('simplesitemap', 'id', 'id', array(
'type' => 'int',
'size' => 'small',
'not null' => TRUE,
));
\Drupal::service('config.factory')->getEditable('simplesitemap.settings')
->set('settings', array('max_links' => 2000))->save();
return t('Before the sitemap can be accessed, it must be regenerated manually or via cron run.');
}
/**
* Modifying database structure to accommodate sitemap chunk timestamps.
*/
function simplesitemap_update_8105() {
db_add_field('simplesitemap', 'generated', array(
'description' => 'Timestamp of sitemap chunk generation.',
'type' => 'int',
'default' => 0,
'not null' => TRUE,
));
}
/**
* Changing column name to 'sitemap_created', as 'generated' is a reserved word since MySQL 5.7.6.
*/
function simplesitemap_update_8106() {
// Omitting the high level DB API to be able to escape the column name.
Drupal\Core\Database\Database::getConnection()
->query("alter table {simplesitemap} CHANGE `generated` sitemap_created int(11) NOT NULL");
}
/**
* Setting the new cron generation setting to TRUE by default.
*/
function simplesitemap_update_8107() {
\Drupal::service('config.factory')->getEditable('simplesitemap.settings')
->set('settings', array('cron_generate' => TRUE))->save();
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>, <a href='@taxonomy_vocabulary_url' target='_blank'>taxonomy vocabulary</a>, <a href='@menu_url' target='_blank'>menu</a> and <a href='@user_url' target='_blank'>user</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", '@menu_url' => "$base_url/admin/structure/menu", '@user_url' => "$base_url/admin/config/people/accounts", '@config_url' => "$base_url/admin/config/search/simple_sitemap/custom")));
}
fieldsetSummaries:
version: VERSION
js:
js/simplesitemap.fieldsetSummaries.js: {}
js/simple_sitemap.fieldsetSummaries.js: {}
dependencies:
- core/jquery
form:
version: VERSION
js:
js/simplesitemap.form.js: {}
js/simple_sitemap.form.js: {}
dependencies:
- core/jquery
simplesitemap.settings:
simple_sitemap.settings:
title: 'Simple XML sitemap'
description: 'Configure and generate the XML sitemap, add custom links to it.'
parent: system.admin_config_search
route_name: simplesitemap.settings
route_name: simple_sitemap.settings
simple_sitemap.settings:
route_name: simple_sitemap.settings
title: 'Settings'
base_route: simple_sitemap.settings
weight: -1
simple_sitemap.settings_custom:
route_name: simple_sitemap.settings_custom
title: 'Custom links'
base_route: simple_sitemap.settings
......@@ -4,15 +4,15 @@
* Main module file containing hooks.
*/
use Drupal\simplesitemap\Simplesitemap;
use Drupal\simplesitemap\SitemapGenerator;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\SitemapGenerator;
/**
* Implements hook_form_alter.
*
* Adds sitemap settings to entity types that are supported via plugins.
*/
function simplesitemap_form_alter(&$form, $form_state, $form_id) {
function simple_sitemap_form_alter(&$form, $form_state, $form_id) {
$sitemap_form_entity_data = Simplesitemap::get_sitemap_form_entity_data($form_state, $form_id);
if (empty($sitemap_form_entity_data))
return;
......@@ -24,9 +24,7 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_config('entity_types');
$form['simplesitemap'] = array(
$form['simple_sitemap'] = array(
'#type' => 'details',
'#group' => 'additional_settings',
'#title' => t('Simple XML sitemap'),
......@@ -34,20 +32,20 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
// Attach some js magic to forms.
// Only attach fieldset summary js to 'additional settings' vertical tabs.
$form['simplesitemap']['#attached']['library'][] = 'simplesitemap/form';
$form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/form';
if (isset($form['additional_settings'])) {
$form['simplesitemap']['#attached']['library'][] = 'simplesitemap/fieldsetSummaries';
$form['simple_sitemap']['#attached']['library'][] = 'simple_sitemap/fieldsetSummaries';
}
$index_content_checked = isset($entity_types[$entity_type_id][$bundle_name]['index']) ? $entity_types[$entity_type_id][$bundle_name]['index'] : FALSE;
$form['simplesitemap']['simplesitemap_index_content'] = array(
$form['simple_sitemap']['simple_sitemap_index_content'] = array(
'#type' => 'checkbox',
'#title' => t('Index content of this type'),
'#default_value' => $index_content_checked,
);
$priority = isset($entity_types[$entity_type_id][$bundle_name]['priority']) ? $entity_types[$entity_type_id][$bundle_name]['priority'] : SitemapGenerator::PRIORITY_DEFAULT;
$form['simplesitemap']['simplesitemap_priority'] = array(
$form['simple_sitemap']['simple_sitemap_priority'] = array(
'#type' => 'select',
'#title' => t('Priority'),
'#description' => t('The priority entities of this bundle will have in the eyes of search engine bots.'),
......@@ -55,52 +53,52 @@ function simplesitemap_form_alter(&$form, $form_state, $form_id) {
'#default_value' => $priority,
);
$form['simplesitemap']['simplesitemap_regenerate_now'] = array(
$form['simple_sitemap']['simple_sitemap_regenerate_now'] = array(
'#type' => 'checkbox',
'#title' => t('Regenerate sitemap after hitting Save'),
'#description' => t('This setting will regenerate the whole sitemap including the above changes.<br/>Otherwise the sitemap will be rebuilt on next cron run.'),
'#default_value' => FALSE,
);
$form['#simplesitemap']['entity_type_id'] = $entity_type_id;
$form['#simplesitemap']['bundle_name'] = $bundle_name;
$form['#simple_sitemap']['entity_type_id'] = $entity_type_id;
$form['#simple_sitemap']['bundle_name'] = $bundle_name;
// Add submission handler.
if (isset($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = 'simplesitemap_entity_form_submit';
$form['actions']['submit']['#submit'][] = 'simple_sitemap_entity_form_submit';
}
else { // Fix for account page which rendered other submit handlers not usable.
$form['#submit'][] = 'simplesitemap_entity_form_submit';
$form['#submit'][] = 'simple_sitemap_entity_form_submit';
}
}
/**
* Form submission handler called in hook_form_alter.
*/
function simplesitemap_entity_form_submit($form, &$form_state) {
function simple_sitemap_entity_form_submit($form, &$form_state) {
$sitemap = new Simplesitemap;
$values = $form_state->getValues();
// Only make changes in DB if sitemap settings actually changed.
if ($values['simplesitemap_index_content'] != $form['simplesitemap']['simplesitemap_index_content']['#default_value']
|| $values['simplesitemap_priority'] != $form['simplesitemap']['simplesitemap_priority']['#default_value']) {
if ($values['simple_sitemap_index_content'] != $form['simple_sitemap']['simple_sitemap_index_content']['#default_value']
|| $values['simple_sitemap_priority'] != $form['simple_sitemap']['simple_sitemap_priority']['#default_value']) {
$entity_type_id = $form['#simplesitemap']['entity_type_id'];
$bundle_name = $form['#simplesitemap']['bundle_name'];
$entity_type_id = $form['#simple_sitemap']['entity_type_id'];
$bundle_name = $form['#simple_sitemap']['bundle_name'];
// Get current entity type sitemap settings.
$entity_types = $sitemap->get_config('entity_types');
$entity_types[$entity_type_id][$bundle_name]['index'] = $values['simplesitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simplesitemap_priority'];
$entity_types[$entity_type_id][$bundle_name]['index'] = $values['simple_sitemap_index_content'];
$entity_types[$entity_type_id][$bundle_name]['priority'] = $values['simple_sitemap_priority'];
// Save new entity type settings.
$sitemap->save_config('entity_types', $entity_types);
}
// Regenerate sitemaps according to user setting.
if ($values['simplesitemap_regenerate_now']) {
if ($values['simple_sitemap_regenerate_now']) {
$sitemap->generate_sitemap();
}
}
......@@ -108,11 +106,11 @@ function simplesitemap_entity_form_submit($form, &$form_state) {
/**
* Implements hook_cron.
*/
function simplesitemap_cron() {
function simple_sitemap_cron() {
$sitemap = new Simplesitemap;
if ($sitemap->get_setting('cron_generate')) {
// Regenerate sitemap for all languages.
$sitemap->generate_sitemap();
$sitemap->generate_sitemap('cron');
}
}
......@@ -121,7 +119,7 @@ function simplesitemap_cron() {
*
* Removes settings of the removed bundle.
*/
function simplesitemap_entity_bundle_delete($entity_type_id, $bundle) {
function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap = new Simplesitemap;
$entity_types = $sitemap->get_config('entity_types');
$bundle_entity_type_id = \Drupal::entityManager()->getDefinition($entity_type_id)->getBundleEntityType();
......@@ -131,6 +129,6 @@ function simplesitemap_entity_bundle_delete($entity_type_id, $bundle) {
$sitemap->save_config('entity_types', $entity_types);
drupal_set_message(t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.
Otherwise it will be rebuilt on the next cron run.",
array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap')));
array('@url' => $GLOBALS['base_url'] . '/admin/config/search/simple_sitemap')));
}
}
simplesitemap.sitemap:
simple_sitemap.sitemap:
path: '/sitemap.xml'
defaults:
_controller: '\Drupal\simplesitemap\Controller\SimplesitemapController::get_sitemap'
_controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::get_sitemap'
requirements:
_access: 'TRUE'
simplesitemap.chunk:
simple_sitemap.chunk:
path: '/sitemaps/{sitemap_id}/sitemap.xml'
defaults:
_controller: '\Drupal\simplesitemap\Controller\SimplesitemapController::get_sitemap'
_controller: '\Drupal\simple_sitemap\Controller\SimplesitemapController::get_sitemap'
requirements:
_access: 'TRUE'
simplesitemap.settings:
path: '/admin/config/search/simplesitemap'
simple_sitemap.settings:
path: '/admin/config/search/simple_sitemap'
defaults:
_form: '\Drupal\simplesitemap\Form\SimplesitemapSettingsForm'
_form: '\Drupal\simple_sitemap\Form\SimplesitemapSettingsForm'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'administer sitemap settings'
simplesitemap.settings_custom:
path: '/admin/config/search/simplesitemap/custom'
simple_sitemap.settings_custom:
path: '/admin/config/search/simple_sitemap/custom'
defaults:
_form: '\Drupal\simplesitemap\Form\SimplesitemapCustomLinksForm'
_form: '\Drupal\simple_sitemap\Form\SimplesitemapCustomLinksForm'
_title: 'Simple XML Sitemap Settings'
requirements:
_permission: 'administer sitemap settings'
services:
plugin.manager.simplesitemap:
class: Drupal\simplesitemap\SimplesitemapManager
plugin.manager.simple_sitemap:
class: Drupal\simple_sitemap\SimplesitemapManager
parent: default_plugin_manager
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
<?php
/**
* @file
* Contains \Drupal\simplesitemap\Annotation\LinkGenerator.
* Contains \Drupal\simple_sitemap\Annotation\LinkGenerator.
*/
namespace Drupal\simplesitemap\Annotation;
namespace Drupal\simple_sitemap\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a LinkGenerator item annotation object.
*
* @see \Drupal\simplesitemap\Plugin\SimplesitemapManager
* @see \Drupal\simple_sitemap\Plugin\SimplesitemapManager
* @see plugin_api
*
* @Annotation
......
<?php
/**
* @file
* Contains \Drupal\simple_sitemap\Batch.
*/
namespace Drupal\simple_sitemap;
use Drupal\user\Entity\User;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
class Batch {
private $batch;
private $batch_info;
const PLUGIN_ERROR_MESSAGE = "The simple_sitemap @plugin plugin has been omitted, as it does not return the required numeric array of path data sets. Each data sets must contain the required path element (relative path string or Drupal\\Core\\Url object) and optionally other elements, like lastmod.";
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;
function __construct($from = 'form') {
$this->batch = array(
'title' => t('Generating XML sitemap'),
'init_message' => t('Initializing batch...'),
'error_message' => t('An error occurred'),
'progress_message' => t('Processing @current out of @total link types.'),
'operations' => array(),
'finished' => __CLASS__ . '::finish_batch',
);
$config = \Drupal::config('simple_sitemap.settings')->get('settings');
$this->batch_info = array(
'from' => $from,
'batch_process_limit' => $config['batch_process_limit'],
'max_links' => $config['max_links'],
'remove_duplicates' => $config['remove_duplicates'],
);
}
public function start() {
batch_set($this->batch);
switch ($this->batch_info['from']) {
case 'form':
break;
case 'drush':
case 'cron':
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
//drush_backend_batch_process(); // Seems to be working, but process does not stop.
batch_process(); // Works, but less fancy.
break;
}
}
public function add_operations($type, $operations) {
switch ($type) {
case 'entity_types':
foreach ($operations as $operation) {
$this->batch['operations'][] = array(
__CLASS__ . '::generate_bundle_urls',
array($operation['query'], $operation['info'], $this->batch_info)
);
};
break;
case 'custom_paths':
$this->batch['operations'][] = array(
__CLASS__ . '::generate_custom_urls',
array($operations, $this->batch_info)
);
break;
}
}
public static function finish_batch($success, $results, $operations) {
if ($success) {
if (!empty($results) || is_null(db_query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField())) {
SitemapGenerator::generate_sitemap($results);
}
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')));
}
else {
}
}
/**
* Wrapper function for Drupal\Core\Url::fromRoute.
* Returns url data for every language.
*
* @param $route_name
* @param $route_parameters
* @param $options
* @see https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Url.php/function/Url%3A%3AfromRoute/8
*
* @return array
* Returns an array containing the internal path, url objects for every language,
* url options and access information.
*/
public static function generate_bundle_urls($query, $info, $batch_info, &$context) {
$languages = \Drupal::languageManager()->getLanguages();
$default_language_id = Simplesitemap::get_default_lang_id();
// Getting id field name from plugin info, if not defined assuming the name of the first field in the query to be the entity id field name.
$fields = $query->getFields();
if (isset($info['field_info']['entity_id']) && isset($fields[$info['field_info']['entity_id']])) {
$id_field = $info['field_info']['entity_id'];
}
else {
reset($fields);
$id_field = key($fields);
}
// Getting the name of the route name field if any.
if (!empty($info['field_info']['route_name'])) {
$route_name_field = $info['field_info']['route_name'];
}
// Initializing batch.
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = $query->countQuery()
->execute()
->fetchField();
}
// Creating a query limited to n=batch_process_limit entries.
$result = $query->condition($id_field, $context['sandbox']['current_id'], '>')
->orderBy($id_field)
->range(0, $batch_info['batch_process_limit'])
->execute()
->fetchAll();
foreach ($result as $row) {
$context['sandbox']['current_id'] = $row->$id_field;
$context['sandbox']['progress']++;
// Getting the name of the route parameter field if any.
if (!empty($info['field_info']['route_parameters'])) {
$route_params_field = $info['field_info']['route_parameters'];
}
// Setting route parameters if they exist in the database (menu links).
if (isset($route_params_field) && !empty($route_parameters = unserialize($row->$route_params_field))) {
$route_parameters = array(key($route_parameters) => $route_parameters[key($route_parameters)]);
}
elseif (!empty($info['path_info']['entity_type'])) {
$route_parameters = array($info['path_info']['entity_type'] => $row->$id_field);
}
else {
$route_parameters = array();
}
// Getting the name of the options field if any.
if (!empty($info['field_info']['options'])) {
$options_field = $info['field_info']['options'];
}
// Setting options if they exist in the database (menu links)
$options = isset($options_field) && !empty($options = unserialize($row->$options_field)) ? $options : array();
$options['absolute'] = TRUE;
// Setting route name if it exists in the database (menu links)
if (isset($route_name_field)) {
$route_name = $row->$route_name_field;
}
elseif (isset($info['path_info']['route_name'])) {
$route_name = $info['path_info']['route_name'];
}
else {
continue;
}