Snippets for eZ publish 3: Copying objects


For all eZ publish versions prior to 3.8, replace




Code sample

The following code copies an existing object and gives it one location in the node tree. It was taken from the copy view of the content module (kernel/content/copy.php).

The used variables are:

  • $object: instance of eZContentObject you want to copy
  • $newParentNodeID: node id of the parent for the new object
  • $allVersions: true if you want to copy all versions of the object to the new object, false if you only want to copy the currently published version
include_once( "lib/ezdb/classes/ezdb.php" );
$db =& eZDB::instance();
$newObject =& $object->copy( $allVersions );

$curVersion        =& $newObject->attribute( 'current_version' );
$curVersionObject  =& $newObject->attribute( 'current' );
$newObjAssignments =& $curVersionObject->attribute( 'node_assignments' );
unset( $curVersionObject );

// remove old node assignments
foreach( $newObjAssignments as $assignment )

// and create a new one
$nodeAssignment = eZNodeAssignment::create( array(
                                                 'contentobject_id' => $newObject->attribute( 'id' ),
                                                 'contentobject_version' => $curVersion,
                                                 'parent_node' => $newParentNodeID,
                                                 'is_main' => 1
                                                 ) );

// publish the newly created object
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
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 );