eZ Community » Forums » Developer » Improved code for adding a node in...
expandshrink

Improved code for adding a node in multiple locations

Improved code for adding a node in multiple locations

Thursday 16 November 2006 2:20:47 pm - 2 replies

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

Thursday 16 November 2006 2:47:29 pm

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.

Tuesday 21 November 2006 1:30:35 pm

Kristof,

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

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from