5 * Module install and update procedures.
9 * Implements hook_requirements().
11 function simple_sitemap_requirements($phase) {
14 if (!extension_loaded('xmlwriter')) {
15 $requirements['simple_sitemap_php_extensions'] = [
16 'title' => t('Simple XML sitemap PHP extensions'),
17 'value' => t('Missing PHP xmlwriter extension'),
18 '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."),
19 'severity' => REQUIREMENT_ERROR,
27 $generator = \Drupal::service('simple_sitemap.generator');
28 $generated_ago = $generator->getGeneratedAgo();
29 $cron_generation = $generator->getSetting('cron_generate');
31 if (!$generated_ago) {
32 $value = t('Not available');
33 $description = t($cron_generation
34 ? "Run cron, or <a href='@generate'>generate</a> the sitemap manually."
35 : "Generation on cron run is disabled. <a href='@generate'>Generate</a> the sitemap manually.", [
36 '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
39 $severity = REQUIREMENT_WARNING;
42 $value = t('XML sitemap is available');
43 $description = t("The <a href='@sitemap'>XML sitemap</a> was generated @ago ago."
44 . ' ' . ($cron_generation
45 ? "Run cron, or <a href='@generate'>regenerate</a> the sitemap manually."
46 : "Generation on cron run is disabled. <a href='@generate'>Regenerate</a> the sitemap manually."), [
47 '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml',
48 '@ago' => $generated_ago,
49 '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
52 $severity = REQUIREMENT_INFO;
55 $requirements['simple_sitemap_generated'] = [
56 'title' => 'Simple XML sitemap',
58 'description' => $description,
59 'severity' => $severity,
67 * Implements hook_schema().
69 function simple_sitemap_schema() {
70 $schema['simple_sitemap'] = [
71 'description' => 'Holds XML sitemaps as strings for quick retrieval.',
74 'description' => 'Sitemap chunk unique identifier.',
81 'description' => 'XML sitemap chunk string.',
86 'sitemap_created' => [
87 'description' => 'Timestamp of sitemap chunk generation.',
94 'primary key' => ['id'],
97 $schema['simple_sitemap_entity_overrides'] = [
98 'description' => 'Holds sitemap settings overridden by entities.',
101 'description' => 'Override unique identifier.',
107 'description' => 'Entity type of the overriding entity.',
113 'description' => 'ID of the overriding entity.',
118 'inclusion_settings' => [
119 'description' => 'Setting for the overriding entity.',
123 'primary key' => ['id'],
129 * Implements hook_install().
131 function simple_sitemap_install() {
132 $base_url = $GLOBALS['base_url'];
133 drupal_set_message(t("You can now include content into the sitemap by visiting the corresponding entity type edit pages (e.g. <a href='@content_type_url'>node type edit pages</a>).<br/>Support for additional entity types and custom links can be added on <a href='@config_url'>the module's configuration pages</a>.", ['@content_type_url' => "$base_url/admin/structure/types", '@config_url' => "$base_url/admin/config/search/simplesitemap"]));
137 * Changing the data structure of the module's configuration.
139 function simple_sitemap_update_8201() {
140 $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
141 $entity_types = is_array($entity_types) ? $entity_types : [];
143 'node_type' => 'node',
144 'taxonomy_vocabulary' => 'taxonomy_term',
145 'menu' => 'menu_link_content',
146 'commerce_product_type' => 'commerce_product',
147 'media_bundle' => 'media',
149 foreach ($entity_types as $entity_type_name => $settings) {
150 if (isset($naming_changes[$entity_type_name])) {
151 $entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
152 unset($entity_types[$entity_type_name]);
155 \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
156 ->set('entity_types', $entity_types)->save();
160 * Moving entity overrides from configuration to database table.
162 function simple_sitemap_update_8202() {
163 $database = \Drupal::database();
165 // Create database table.
166 if (!$database->schema()->tableExists('simple_sitemap_entity_overrides')) {
167 $database->schema()->createTable('simple_sitemap_entity_overrides', [
168 'description' => 'Holds sitemap settings overridden by entities.',
171 'description' => 'Override unique identifier.',
177 'description' => 'Entity type of the overriding entity.',
183 'description' => 'ID of the overriding entity.',
188 'inclusion_settings' => [
189 'description' => 'Setting for the overriding entity.',
193 'primary key' => ['id'],
197 // Populate database table with config values.
198 $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
199 $entity_types = is_array($entity_types) ? $entity_types : [];
201 foreach ($entity_types as $entity_type_name => &$entity_type) {
202 if (is_array($entity_type)) {
203 foreach($entity_type as $bundle_name => &$bundle) {
204 if (isset($bundle['entities'])) {
205 foreach($bundle['entities'] as $entity_id => $entity_settings) {
206 $database->insert('simple_sitemap_entity_overrides')
208 'entity_type' => $entity_type_name,
209 'entity_id' => $entity_id,
210 'inclusion_settings' => serialize($entity_settings),
214 // Remove entity overrides from configuration.
215 unset($bundle['entities']);
221 \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
222 ->set('entity_types', $entity_types)->save();
226 * Splitting simple_sitemap.settings configuration into simple_sitemap.settings,
227 * simple_sitemap.entity_types and simple_sitemap.custom.
229 function simple_sitemap_update_8203() {
230 $old_config = $config = \Drupal::config('simple_sitemap.settings');
231 foreach (['entity_types', 'custom'] as $config_name) {
232 if (!$config = $old_config->get($config_name)) {
235 \Drupal::service('config.factory')->getEditable("simple_sitemap.$config_name")
236 ->setData($config)->save();
238 $settings = $old_config->get('settings');
239 \Drupal::service('config.factory')->getEditable("simple_sitemap.settings")
240 ->setData($settings)->save();
244 * Removing entity type settings for entity types which do not have the canonical
247 function simple_sitemap_update_8204() {
248 $sitemap_entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
249 $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
250 unset($entity_types['_core']);
251 foreach($entity_types as $entity_type_id => $entity_type) {
252 if (!isset($sitemap_entity_types[$entity_type_id])
253 || !$sitemap_entity_types[$entity_type_id]->hasLinkTemplate('canonical')) {
255 // Delete entity overrides.
256 \Drupal::database()->delete('simple_sitemap_entity_overrides')
257 ->condition('entity_type', $entity_type_id)
260 // Delete entity type settings.
261 unset($entity_types[$entity_type_id]);
264 \Drupal::service('config.factory')->getEditable("simple_sitemap.entity_types")
265 ->setData($entity_types)->save();
269 * Splitting simple_sitemap.entity_types into individual configuration objects
272 function simple_sitemap_update_8205() {
273 $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
274 unset($entity_types['_core']);
275 $enabled_entity_types = [];
276 foreach($entity_types as $entity_type_id => $bundles) {
277 $enabled_entity_types[] = $entity_type_id;
278 foreach($bundles as $bundle_name => $bundle_settings) {
279 \Drupal::service('config.factory')
280 ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
281 ->setData($bundle_settings)->save();
285 // Add enabled entity type settings.
286 \Drupal::service('config.factory')
287 ->getEditable('simple_sitemap.settings')
288 ->set('enabled_entity_types', $enabled_entity_types)
291 // Remove old configuration object.
292 \Drupal::service('config.factory')
293 ->getEditable('simple_sitemap.entity_types')
298 * Placing custom links in a subkey of simple_sitemap.custom configuration.
300 function simple_sitemap_update_8206() {
301 $custom_links = \Drupal::config('simple_sitemap.custom')->get();
302 foreach($custom_links as $i => $custom_link) {
303 if (!isset($custom_link['path'])) {
304 unset($custom_links[$i]);
307 \Drupal::service('config.factory')->getEditable("simple_sitemap.custom")
308 ->setData(['links' => $custom_links])->save();
312 * Updating entity_id field of simple_sitemap_entity_overrides table to varchar(32).
314 function simple_sitemap_update_8207() {
315 \Drupal::database()->schema()->changeField(
316 'simple_sitemap_entity_overrides',
319 'description' => 'ID of the overriding entity.',