Skip to content
SA-CONTRIB-2018-053-6.x-2.x.patch 1.31 KiB
Newer Older
diff --git a/xmlsitemap_node/xmlsitemap_node.module b/xmlsitemap_node/xmlsitemap_node.module
index 4a084cc..73abdf5 100644
--- a/xmlsitemap_node/xmlsitemap_node.module
+++ b/xmlsitemap_node/xmlsitemap_node.module
@@ -81,8 +81,8 @@ function xmlsitemap_node_nodeapi(stdClass &$node, $op, $a3 = NULL, $a4 = NULL) {
   switch ($op) {
     case 'insert':
     case 'update':
-      $link = xmlsitemap_node_create_link($node);
-      xmlsitemap_link_save($link);
+      // Defer creating and saving the link until the node is fully saved.
+      register_shutdown_function('xmlsitemap_node_create_and_save_link', $node, getcwd());
       break;
     case 'delete':
       xmlsitemap_link_delete('node', $node->nid);
@@ -236,6 +236,21 @@ function xmlsitemap_node_create_link(stdClass $node) {
 }
 
 /**
+ * Create and save a sitemap link from a node.
+ */
+function xmlsitemap_node_create_and_save_link($node, $drupal_root = NULL) {
+  if (!empty($drupal_root)) {
+    // Set the CWD to DRUPAL_ROOT as it is not guaranteed to be the same as it
+    // was in the normal context of execution.
+    // @see _drupal_shutdown_function() from Drupal 7.
+    chdir($drupal_root);
+  }
+
+  $link = xmlsitemap_node_create_link($node);
+  xmlsitemap_link_save($link);
+}
+
+/**
  * Determine whether a user may view the specified node.
  *
  * @param $node