Forums / Developer / Improved code for adding a node in multiple locations

Improved code for adding a node in multiple locations

Author Message

Ammar Ibrahim

Thursday 16 November 2006 5:20:47 am

Using the new custom edit handler (http://serwatka.net/en/blog/ez_publish_3_8_new_custom_edit_handler), I managed to solve a problem while designing our new CMS.

    function publish( $contentObjectID, $contentObjectVersion )
    {
    	
    	//To check whether the node was already assigned or not
    	$isAssigned = false;
    	
    	// fetch object
        $object =& eZContentObject::fetch( $contentObjectID );
        // get content class object
        $contentClass =& $object->attribute('content_class');
        
        
        
        // check if currently published object is Article
        if ( $contentClass->attribute('id') == 16 )
        {
            include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
 
            // prepare new object data
            $parentNodeID = 69;
            
            //check if this Node is already assigned to the destination node
            $assignedNodes = $object->attribute('assigned_nodes');
            foreach ($assignedNodes as $assignedNode){
            	if( $parentNodeID == $assignedNode->ParentNodeID ){
            		$isAssigned = true;
            		break;
            	}
            }
            
            if( !$isAssigned ){
				$nodeAssignment =& eZNodeAssignment::create( 			
								array(
									'contentobject_id' => $object->attribute('id'),
									'contentobject_version' => $contentObjectVersion,
									'parent_node' => $parentNodeID,
									'is_main' => 0
								)
							);

				//print_r($nodeAssignment);
				$nodeAssignment->store();
				$operationResult = eZOperationHandler::execute( 'content', 'publish', 
									array( 
										'object_id' => $object->attribute( 'id' ), 
										'version' => $contentObjectVersion, 
									) 
								); 
            }  
			
        }
 
    }

The important thing to notice, is that I'm checking if the node is already assigned. If this check wasn't placed the the system would go in an infinite loop

Kristof Coomans

Thursday 16 November 2006 5:47:29 am

Hi
You're actually executing the publish operation inside the publish operation of the same object / version. I would watch out doing stuff like this.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Ammar Ibrahim

Tuesday 21 November 2006 4:30:35 am

Kristof,

Thanks for the reply. What's a better way to do it then?