Skip to content

Instantly share code, notes, and snippets.

@pauletienney
Created January 17, 2013 16:54
Show Gist options
  • Save pauletienney/4557479 to your computer and use it in GitHub Desktop.
Save pauletienney/4557479 to your computer and use it in GitHub Desktop.
eZ Publish 4 : copy an object and put it in a new node
function copyObjectToNewLocation($object, $newParentNodeId) {
$db = eZDB::instance();
$db->begin();
$newObject = $object->copy(false);
$newObject->setAttribute( 'section_id', 0 );
$newObject->store();
$curVersion = $newObject->attribute( 'current_version' );
$curVersionObject = $newObject->attribute( 'current' );
$newObjAssignments = $curVersionObject->attribute( 'node_assignments' );
unset( $curVersionObject );
foreach( $newObjAssignments as $assignment )
{
$assignment->purge();
}
$nodeAssignment = eZNodeAssignment::create( array(
'contentobject_id' => $newObject->attribute( 'id' ),
'contentobject_version' => $curVersion,
'parent_node' => $newParentNodeId,
'is_main' => 1
) );
eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $newObject->attribute( 'id' ),
'version' => $curVersion ) );
// Update "is_invisible" attribute for the newly created node.
$newNode = $newObject->attribute( 'main_node' );
eZContentObjectTreeNode::updateNodeVisibility( $newNode, $newParentNode );
$db->commit();
return $newObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment