5 * Module install and update procedures.
9 * Implements hook_requirements().
14 function simple_sitemap_requirements($phase) {
17 if (!extension_loaded('xmlwriter')) {
18 $requirements['simple_sitemap_php_extensions'] = [
19 'title' => t('Simple XML sitemap PHP extensions'),
20 'value' => t('Missing PHP xmlwriter extension'),
21 '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.'),
22 'severity' => REQUIREMENT_ERROR,
30 $generator = \Drupal::service('simple_sitemap.generator');
31 $generated_ago = $generator->getGeneratedAgo();
32 $cron_generation = $generator->getSetting('cron_generate');
34 if (!$generated_ago) {
35 $value = t('Not available');
36 $description = t($cron_generation
37 ? 'Run cron, or <a href="@generate">generate</a> the sitemap manually.'
38 : 'Generation on cron run is disabled. <a href="@generate">Generate</a> the sitemap manually.', [
39 '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
42 $severity = REQUIREMENT_WARNING;
45 $value = t('XML sitemap is available');
46 $description = t('The <a href="@sitemap">XML sitemap</a> was generated @ago ago.'
47 . ' ' . ($cron_generation
48 ? 'Run cron, or <a href="@generate">regenerate</a> the sitemap manually.'
49 : 'Generation on cron run is disabled. <a href="@generate">Regenerate</a> the sitemap manually.'), [
50 '@sitemap' => $GLOBALS['base_url'] . '/sitemap.xml',
51 '@ago' => $generated_ago,
52 '@generate' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap'
55 $severity = REQUIREMENT_INFO;
58 $requirements['simple_sitemap_generated'] = [
59 'title' => 'Simple XML sitemap',
61 'description' => $description,
62 'severity' => $severity,
70 * Implements hook_schema().
72 function simple_sitemap_schema() {
73 $schema['simple_sitemap'] = [
74 'description' => 'Holds XML sitemaps as strings for quick retrieval.',
77 'description' => 'Sitemap chunk unique identifier.',
84 'description' => 'XML sitemap chunk string.',
89 'sitemap_created' => [
90 'description' => 'Timestamp of sitemap chunk generation.',
97 'primary key' => ['id'],
100 $schema['simple_sitemap_entity_overrides'] = [
101 'description' => 'Holds sitemap settings overridden by entities.',
104 'description' => 'Override unique identifier.',
110 'description' => 'Entity type of the overriding entity.',
116 'description' => 'ID of the overriding entity.',
121 'inclusion_settings' => [
122 'description' => 'Setting for the overriding entity.',
126 'primary key' => ['id'],
132 * Implements hook_install().
134 function simple_sitemap_install() {
135 $base_url = $GLOBALS['base_url'];
136 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"]));
140 * Changing the data structure of the module's configuration.
142 function simple_sitemap_update_8201() {
143 $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
144 $entity_types = is_array($entity_types) ? $entity_types : [];
146 'node_type' => 'node',
147 'taxonomy_vocabulary' => 'taxonomy_term',
148 'menu' => 'menu_link_content',
149 'commerce_product_type' => 'commerce_product',
150 'media_bundle' => 'media',
152 foreach ($entity_types as $entity_type_name => $settings) {
153 if (isset($naming_changes[$entity_type_name])) {
154 $entity_types[$naming_changes[$entity_type_name]] = $entity_types[$entity_type_name];
155 unset($entity_types[$entity_type_name]);
158 \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
159 ->set('entity_types', $entity_types)->save();
163 * Moving entity overrides from configuration to database table.
165 function simple_sitemap_update_8202() {
166 $database = \Drupal::database();
168 // Create database table.
169 if (!$database->schema()->tableExists('simple_sitemap_entity_overrides')) {
170 $database->schema()->createTable('simple_sitemap_entity_overrides', [
171 'description' => 'Holds sitemap settings overridden by entities.',
174 'description' => 'Override unique identifier.',
180 'description' => 'Entity type of the overriding entity.',
186 'description' => 'ID of the overriding entity.',
191 'inclusion_settings' => [
192 'description' => 'Setting for the overriding entity.',
196 'primary key' => ['id'],
200 // Populate database table with config values.
201 $entity_types = \Drupal::config('simple_sitemap.settings')->get('entity_types');
202 $entity_types = is_array($entity_types) ? $entity_types : [];
204 foreach ($entity_types as $entity_type_name => &$entity_type) {
205 if (is_array($entity_type)) {
206 foreach ($entity_type as $bundle_name => &$bundle) {
207 if (isset($bundle['entities'])) {
208 foreach ($bundle['entities'] as $entity_id => $entity_settings) {
209 $database->insert('simple_sitemap_entity_overrides')
211 'entity_type' => $entity_type_name,
212 'entity_id' => $entity_id,
213 'inclusion_settings' => serialize($entity_settings),
217 // Remove entity overrides from configuration.
218 unset($bundle['entities']);
224 \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
225 ->set('entity_types', $entity_types)->save();
229 * Splitting simple_sitemap.settings configuration into simple_sitemap.settings,
230 * simple_sitemap.entity_types and simple_sitemap.custom.
232 function simple_sitemap_update_8203() {
233 $old_config = $config = \Drupal::config('simple_sitemap.settings');
234 foreach (['entity_types', 'custom'] as $config_name) {
235 if (!$config = $old_config->get($config_name)) {
238 \Drupal::service('config.factory')->getEditable("simple_sitemap.$config_name")
239 ->setData($config)->save();
241 $settings = $old_config->get('settings');
242 \Drupal::service('config.factory')->getEditable("simple_sitemap.settings")
243 ->setData($settings)->save();
247 * Removing entity type settings for entity types which do not have the canonical
250 function simple_sitemap_update_8204() {
251 $sitemap_entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
252 $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
253 unset($entity_types['_core']);
254 foreach ($entity_types as $entity_type_id => $entity_type) {
255 if (!isset($sitemap_entity_types[$entity_type_id])
256 || !$sitemap_entity_types[$entity_type_id]->hasLinkTemplate('canonical')) {
258 // Delete entity overrides.
259 \Drupal::database()->delete('simple_sitemap_entity_overrides')
260 ->condition('entity_type', $entity_type_id)
263 // Delete entity type settings.
264 unset($entity_types[$entity_type_id]);
267 \Drupal::service('config.factory')->getEditable("simple_sitemap.entity_types")
268 ->setData($entity_types)->save();
272 * Splitting simple_sitemap.entity_types into individual configuration objects
275 function simple_sitemap_update_8205() {
276 $entity_types = \Drupal::config('simple_sitemap.entity_types')->get();
277 unset($entity_types['_core']);
278 $enabled_entity_types = [];
279 foreach ($entity_types as $entity_type_id => $bundles) {
280 $enabled_entity_types[] = $entity_type_id;
281 foreach ($bundles as $bundle_name => $bundle_settings) {
282 \Drupal::service('config.factory')
283 ->getEditable("simple_sitemap.bundle_settings.$entity_type_id.$bundle_name")
284 ->setData($bundle_settings)->save();
288 // Add enabled entity type settings.
289 \Drupal::service('config.factory')
290 ->getEditable('simple_sitemap.settings')
291 ->set('enabled_entity_types', $enabled_entity_types)
294 // Remove old configuration object.
295 \Drupal::service('config.factory')
296 ->getEditable('simple_sitemap.entity_types')
301 * Placing custom links in a subkey of simple_sitemap.custom configuration.
303 function simple_sitemap_update_8206() {
304 $custom_links = \Drupal::config('simple_sitemap.custom')->get();
305 foreach ($custom_links as $i => $custom_link) {
306 if (!isset($custom_link['path'])) {
307 unset($custom_links[$i]);
310 \Drupal::service('config.factory')->getEditable('simple_sitemap.custom')
311 ->setData(['links' => $custom_links])->save();
315 * Updating entity_id field of simple_sitemap_entity_overrides table to varchar(32).
317 function simple_sitemap_update_8207() {
318 \Drupal::database()->schema()->changeField(
319 'simple_sitemap_entity_overrides',
322 'description' => 'ID of the overriding entity.',
331 * Adding changefreq setting to all existing bundle and entity instance settings.
333 function simple_sitemap_update_8208() {
335 // Update existing bundle settings.
336 $config_factory = \Drupal::service('config.factory');
337 $entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');
339 foreach ($entity_types as $entity_type) {
340 $config = $config_factory->get($entity_type)->get();
341 if (!isset($config['changefreq'])) {
342 $config_factory->getEditable($entity_type)
343 ->setData($config + ['changefreq' => ''])
348 // Update existing entity override data.
349 $results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
350 ->fields('o', ['id', 'inclusion_settings'])
351 ->execute()->fetchAll(\PDO::FETCH_OBJ);
353 foreach ($results as $row) {
354 $settings = unserialize($row->inclusion_settings);
355 if (!isset($settings['changefreq'])) {
356 \Drupal::database()->update('simple_sitemap_entity_overrides')
357 ->fields(['inclusion_settings' => serialize($settings + ['changefreq' => '']),])
358 ->condition('id', $row->id)
363 return t('You may now want to configure the new changefreq setting for sitemap entities and custom links.');
367 * Adding image inclusion setting to all existing bundle and entity instance settings.
369 function simple_sitemap_update_8209() {
371 // Update existing bundle settings.
372 $config_factory = \Drupal::service('config.factory');
373 $entity_types = $config_factory->listAll('simple_sitemap.bundle_settings.');
375 foreach ($entity_types as $entity_type) {
376 $config = $config_factory->get($entity_type)->get();
377 if (!isset($config['include_images'])) {
378 $config_factory->getEditable($entity_type)
379 ->setData($config + ['include_images' => 0])
384 // Update existing entity override data.
385 $results = \Drupal::database()->select('simple_sitemap_entity_overrides', 'o')
386 ->fields('o', ['id', 'inclusion_settings'])
387 ->execute()->fetchAll(\PDO::FETCH_OBJ);
389 foreach ($results as $row) {
390 $settings = unserialize($row->inclusion_settings);
391 if (!isset($settings['include_images'])) {
392 \Drupal::database()->update('simple_sitemap_entity_overrides')
393 ->fields(['inclusion_settings' => serialize($settings + ['include_images' => 0]),])
394 ->condition('id', $row->id)
399 return t('You may now want to configure your sitemap entities to include images.');