Share » Forums » Developer » Dynamically creating multiple locations

Dynamically creating multiple locations

Dynamically creating multiple locations

Tuesday 10 July 2007 11:41:15 pm - 1 reply

Modified on Tuesday 10 July 2007 11:41:18 pm by Andy Caiger

Author Message

Fabrice PEREZ

Thursday 12 July 2007 12:24:25 am

Hello Andy,

So, to create new object in the content tree:

/* To find the id of the class without stock it in .ini */
$class = eZContentClass::fetchByIdentifier( 'folder' );
$contentObject = $class->instantiateIn( $languageToUse, $userId, $sectionId, false, EZ_VERSION_STATUS_INTERNAL_DRAFT );
/* You create the temp node*/
$node = eZNodeAssignment::create( array( 
             'contentobject_id' => $contentObject->attribute( 'id' ),
	     'contentobject_version' => $contentObject->attribute( 'current_version' ),
	     'parent_node' => $idOfTheParentNode,
	     'is_main' => 1,
	     'sort_field' => $class->attribute( 'sort_field' ),
	     'sort_order' => $class->attribute( 'sort_order' ) ) );
/* Store your temp node */
$node->store();

/* Create the first version */
$version =& $contentObject->version( 1 );
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

/* Set the name of the new object */
$contentObject->setName( $theNameOfTheNewObject );

/* Fetch the datamap of the object to modify the attributes */
$contentObjectDataMap =& $contentObject->dataMap();

/* Set the value of the title */
$contentObjectDataMap['title']->setAttribute( 'data_text', $theNameOfTheNewObject );
$contentObjectDataMap['title']->store();
$contentObject->store();

/* After you publish the new object */
include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 
                                                        'object_id' => $contentObject->attribute( 'id' ),
                                                        'version' => $version->attribute( 'version' ) ) );

Here, you have your new object.
After, if you want to add locations to your object :

$insertedNode =& $contentObject->addLocation( $nodeToLink, true );
$insertedNode->setAttribute( 'contentobject_is_published', 1 );
$insertedNode->setAttribute( 'main_node_id', $contentObject->mainNodeID() );
$insertedNode->setAttribute( 'contentobject_version', $contentObject->attribute('current_version') );
$insertedNode->updateSubTreePath();
$insertedNode->sync();

That's all.
Hope this help

Fabrice, Internethic

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

36 542 Users on board!

Forums menu