Hi. Has anyone used node_clone for cloning a node that has a parent?

I am seeing an issue whereby cloned node does not show up under parent, even though it seemed to clone correctly. Maybe nodehierarchy needs to implement a hook()?

CommentFileSizeAuthor
#6 nodehierarchy-860366.patch11.7 KBbceyssens

Comments

dgorton’s picture

Category: bug » feature
asb’s picture

Title: node_clone module & nodehierarchy » Incompatibility between node_clone and nodehierarchy modules
Priority: Normal » Major

I can confirm this issue, and it's a nasty one.

'Node clone' should either completely duplicate a node with all it's properties, or clone the core fields and drop the properties; e.g. taxonomy terms and attached images might be duplicated, certain CCK fields might be dropped.

With Nodehierarchy 6.x-2.x-dev it's none of both; the parent seems to become copied, but actually it isn't. And worse, if the broken parent is removed and then re-attached to the cloned node, it still doesn't put it in the node hierarchy. Basically cloned nodes are somehow "damaged" and no longer fully operational in a node hierarchy.

tseven’s picture

Component: Code » Drupal/PHP Code

I'd love to see this fixed. Its causing issues on one of my sites.

Does anyone have an idea of how to begin fixing this issue. I have a little time I can dedicate towards fixing it, but it would help if didn't have to start at square 1.

zeezhao’s picture

See my post here on how to get around this:
http://drupal.org/node/621662#comment-3297072

afeldinger’s picture

I've bumped into this issue a couple of times over the last 3-6 months without recognizing it. So today I noticed the connection between errors in the menu tables and cloned nodes and began to investigate.

As best I can tell, the problem is, that the $node->nodehierarchy_menu_links array is reused with all values when cloning a node. Not having a null-value for the mlid property tells menu_link_save to update the existing link, rather then creating a new one.

The node_clone has a hook that will alter the array before either saving or prepopulating the cloned node. This seems to have fixed my issue, but I intend to keep and eye on it. The bug hasn't been very consistent, so it's kind of hard to test whether or not this is working completely.

Anyway, I've implemented the hook in a custom module, but ideally I guess this should be handled in nodehierarchy.module

/* 
 *	Implementation of hook_clone_node_alter
 */
function mymodule_clone_node_alter(&$node, $original_node, $method) {
	
	// fix apparent incompatability between nodehierarchy and node_clone:
	// http://drupal.org/node/860366
	// http://drupal.org/node/621662#comment-3297072
	if ($node->clone_from_original_nid == $original_node->nid) {
		for ($i = 0; $i < count(@$node->nodehierarchy_menu_links); $i++) {
			// create new menu links rather than updating old ones
			$node->nodehierarchy_menu_links[$i] = array('pnid'=>$node->nodehierarchy_menu_links[$i]['pnid']) + _nodehierarchy_default_menu_link(NULL, $node->nodehierarchy_menu_links[$i]['plid']);
		}
	}
}

If anyone else is still experiencing this issue, I'd strongly welcome any input on this issue.

bceyssens’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new11.7 KB
bibo’s picture

This is an old issue, even for D6, but probably still relevant for some on D7 using node clone and node hierarchy.

About the patch in previous comment, it seems pretty long and scary, but its actually mostly just irrelevant whitespace changes (by the editor I guess).
I'm not going to dwell deeper into to details, but the non-irrelevant parts from the patch seem to be:

/**
+ * Implementation of hook_clone_node_alter().
+ */
+function nodehierarchy_clone_node_alter(&$node, $original_node, $method) {
+  // see https://drupal.org/node/860366 for more information about this issue
+  if ($node->clone_from_original_nid == $original_node->nid) {
+    for ($i = 0; $i < count(@$node->nodehierarchy_menu_links); $i++) {
+      // create new menu links rather than updating old ones
+      $node->nodehierarchy_menu_links[$i] = array(
  'pnid'=>$node->nodehierarchy_menu_links[$i]['pnid']) + _nodehierarchy_default_menu_link(NULL, $node->nodehierarchy_menu_links[$i]['plid'], 
  $node->nodehierarchy_menu_links[$i]['enabled'], $node->nodehierarchy_menu_links[$i]['customized'], 
  'Copy of ' . $node->nodehierarchy_menu_links[$i]['link_title'], $node->nodehierarchy_menu_links[$i]['expanded'], 
   $node->nodehierarchy_menu_links[$i]['options']['attributes']['title']);
+    }
+  }
+}

---

 /**
  * Get the default menu link values for a new nodehierarchy menu link.
  */
-function _nodehierarchy_default_menu_link($nid = NULL, $plid = 0, $enabled = FALSE) {
+function _nodehierarchy_default_menu_link($nid = NULL, $plid = 0, $enabled = FALSE, $customized = 0, $link_title = NULL, $expanded = 0, $description = NULL) {

---

@@ -1472,19 +1485,26 @@ function nodehierarchy_delete_node_nodehierarchy_menu_link($mlid) {
 /**
  * Get the default menu link values for a new nodehierarchy menu link.
  */
-function _nodehierarchy_default_menu_link($nid = NULL, $plid = 0, $enabled = FALSE) {
+function _nodehierarchy_default_menu_link($nid = NULL, $plid = 0, $enabled = FALSE, $customized = 0, $link_title = NULL, $expanded = 0, $description = NULL) {
   return array(
     'mlid'        => NULL,
     'module'      => 'nodehierarchy',
     'menu_name'   => variable_get('nodehierarchy_default_menu_name', 'navigation'),
     'router_path' => 'node/%',
     'link_path'   => !empty($nid) ? 'node/'. $nid : '',
+    'link_title' => $link_title,
     'hidden'      => (bool) !$enabled,
     'enabled'     => (bool) $enabled,
     'plid'        => $plid,
     'weight'      => 0,
     'nid'         => !empty($nid) ? $nid : NULL,
-    'customized'  => FALSE,
+    'customized'  => $customized,
+    'expanded'    => $expanded,
+    'options'     => array(
+      'attributes' => array(
+        'title'   => $description,
+      ),
+    ),
   );
 }