Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents /home and /404 pages from being indexed by Simple XML Sitemap #13

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ucb_default_content.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies:
- node
- path_alias
- pathauto
- simple_sitemap
- system
version: '1.3.2'
version: '1.4.0'
package: CU Boulder
50 changes: 48 additions & 2 deletions ucb_default_content.install
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ function _create_home_page() {
Please click the EDIT button at the top of the screen to edit this page.',
]);
$node->enforceIsNew()->save();
\Drupal::configFactory()->getEditable('system.site')->set('page.front', '/node/' . $node->id())->save();
$nid = $node->id();
\Drupal::configFactory()->getEditable('system.site')->set('page.front', '/node/' . $nid)->save();
_set_simplesitemap_no_index($nid);
}

/**
Expand All @@ -67,5 +69,49 @@ function _create_404_page() {
'body' => 'The page you are looking for appears to have been moved, deleted, or does not exist.',
]);
$node->enforceIsNew()->save();
\Drupal::configFactory()->getEditable('system.site')->set('page.404', '/node/' . $node->id())->save();
$nid = $node->id();
\Drupal::configFactory()->getEditable('system.site')->set('page.404', '/node/' . $nid)->save();
_set_simplesitemap_no_index($nid);
}

/**
* Sets a node to not be indexed by Simple XML Sitemap.
*
* @param string $nid
* The id of the node.
*
* @internal
*/
function _set_simplesitemap_no_index($nid) {
// This method may not be part of the public API but there is no other clean
// way of doing this. Simple XML Sitemap still uses a custom
// `simple_sitemap_entity_overrides` DB table to set these overrides. There's
// an issue to store these overrides as fields on the entity but it's been
// open since 2019 (!!!):
// https://www.drupal.org/project/simple_sitemap/issues/3034070
// BEWARE OF BREAKING CHANGES to Simple XML Sitemap.
$database = \Drupal::database();
if ($database->schema()->tableExists('simple_sitemap_entity_overrides')) {
$database->merge('simple_sitemap_entity_overrides')
->keys([
'type' => 'default',
'entity_type' => 'node',
'entity_id' => $nid,
])
->fields([
'type' => 'default',
'entity_type' => 'node',
'entity_id' => $nid,
'inclusion_settings' => serialize([
'index' => '0',
'priority' => '0.5',
'changefreq' => '',
'include_images' => '0',
]),
])
->execute();
}
else {
\Drupal::logger('ucb_default_content')->warning('Failed to set Simple XML Sitemap no index (table doesn\'t exist). Simple XML Sitemap isn\'t installed or changed something in an update.');
}
}
Loading