Create new objects/nodes through PHP

Author Message

Håkan Bergman

Tuesday 04 May 2010 4:19:21 am

Hello,

I am using my own extension that have a module which will create objects.
Basic code:
$contentClassIdentifier = 'article';
$class = eZContentClass::fetchByIdentifier( $contentClassIdentifier );
$ParentNodeID = 2;
$node = eZContentObjectTreeNode::fetch( $ParentNodeID );
$contentClassIdentifier = 'article';
$class = eZContentClass::fetchByIdentifier( $contentClassIdentifier );
$ParentNodeID = 2;
$node = eZContentObjectTreeNode::fetch( $ParentNodeID );

I call class article (attributes) and then fetches node 2 (Home) because I want to place the new article in the Home subdir directory. I am using eznodeassignment to create the object and the object is created with default attributes of the class article. Evereything looks fine.

The only part it does not create data is in table ezcontentobject_tree, and my guess is that is why I can't see the new article in my admin interface. I need some input on how I can add an object, with custom attribute data and in table ezcontentobject_tree so I can view and edit the object in the admin interface.

{* Begin Code *}

------------------------------------------------------------------------------------

include_once( 'kernel/common/template.php' );$contentClassIdentifier = 'article';

$class = eZContentClass::fetchByIdentifier( $contentClassIdentifier );

$ParentNodeID = 2;

$node = eZContentObjectTreeNode::fetch( $ParentNodeID );
if ( is_object( $class ) ){
$contentClassID = $class->attribute( 'id' );

$parentContentObject =& $node->attribute( 'object' );

$accessResult = $parentContentObject->checkAccess( 'create', $contentClassID, $parentContentObject->attribute( 'contentclass_id' ) );

if ( $accessResult == '1' ) {

include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );

$user =& eZUser::currentUser();

$userID =& $user->attribute( 'contentobject_id' );

$sectionID = $parentContentObject->attribute( 'section_id' );

include_once( 'lib/ezdb/classes/ezdb.php' );

$db =& eZDB::instance();

$db->begin();

$contentObject =& $class->instantiate( $userID, $sectionID );

$nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ), 'contentobject_version' => $contentObject->attribute( 'current_version' ), 'parent_node' => $node->attribute( 'node_id' ) ) );

$nodeAssignment->store();

$db->commit();

}

}

------------------------------------------------------------------------------------

{* End Code *}

Håkan Bergman

Wednesday 05 May 2010 12:30:21 am

I tried a different code that will createandpublish:

$Params = array ();
$Params['parent_node_id'] = 92;
$Params['class_identifier'] = 'local_organisation';
$Params['creator_id'] = 14;
$Params['section_id'] = 2;

$AttributesData = array();
$AttributesData['name'] = 'My Company';
$Params['attributes'] = $attributesData;

$ContentObject = eZContentFunctions::createAndPublishObject($Params);

local_organisation is a custom class with one datatype with an identifier called "name".
The attribute have a default identifier "My Company". I have tried to manipulate the attribute data but nothing happens.
Is this a good way to create and publish an object below a parentnode?
Should I do this differently and does anyone know how I can manipulate the datatype data before it submits into the DB?
The code works very nice, but I am concerned if this is done the right way and what I should think of. My main problem right now is how to manipulate data. I have watched a few examples, but nothing works so far when trying to change the name of the attribute "name" from the default value I put in the class.

Best regards,

Håkan

Mark Simon

Wednesday 05 May 2010 1:30:07 am

This looks quite goot, but try to publish the new created object.

When creatind an Object and just storing it, You just have a draft of it.

after $nodeAssignment->store(); instert:

$contentObject->store();
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ), 'version' => 1 ) );

www.all2e.com

Paul Leclercq

Wednesday 05 May 2010 7:38:12 am

There are 2 good ways of publishing new content when creating an import script:

The one you stated in your second post:

exemple:

<span>$params</span> = <a href="http://www.php.net/array" mce_href="http://www.php.net/array"><span>array</span></a><span>(</span><span>)</span>;
<span>$params</span><span>[</span><span>'parent_node_id'</span><span>]</span> = <span>52</span>; <span>// node id of /Media/Files</span>
<span>$params</span><span>[</span><span>'class_identifier'</span><span>]</span> = <span>'file'</span>;
<span>$params</span><span>[</span><span>'creator_id'</span><span>]</span> = <span>14</span>; <span>// admin</span>
<span>$params</span><span>[</span><span>'storage_dir'</span><span>]</span> = <span>'/tmp/data/'</span>; <span>// don't forget the ended /</span>
<span>$params</span><span>[</span><span>'section_id'</span><span>]</span> = <span>3</span>; <span>// section media</span>
 
<span>$attributesData</span> = <a href="http://www.php.net/array" mce_href="http://www.php.net/array"><span>array</span></a><span>(</span><span>)</span>;
<span>$attributesData</span><span>[</span><span>'name'</span><span>]</span> = <span>'My file'</span>;
<span>$attributesData</span><span>[</span><span>'file'</span><span>]</span> = <span>'my_file.txt'</span>;
 
<span>$params</span><span>[</span><span>'attributes'</span><span>]</span> = <span>$attributesData</span>;
<span>$contentObject</span> = eZContentFunctions::<span>createAndPublishObject</span><span>(</span> <span>$params</span> <span>)</span>;

But if I were you, I would base my import script on the following file you will find in your ezpublish installation under:

bin/php/ezcsvimport.php

You will find Marks method in that file, which is the standard method used, and everything you need to create setup your attributes with the value you would like.

All you will need is to setup your $nodeID value.

This is also the code they give you during eZSystem's advanced ezpublish training course:

-----------------------------------

$class = eZContentClass::fetchByIdentifier( 'article' );
$contentObject = $class->instantiate( $creator );
$contentObject->store();

$nodeAssignment = eZNodeAssignment::create( array(
'contentobject_id' => $contentObject->attribute( 'id' ),
'contentobject_version' => $contentObject->attribute( 'current_version' ),
'parent_node' => $nodeID,
'is_main' => 1
)
);
$nodeAssignment->store();

$version = $contentObject->version( 1 );
$version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();

$attributes = $contentObject->attribute( 'contentobject_attributes' );
while ( list( $key, $attribute ) = each( $attributes ) )
{
$dataString = $objectData[$key];
switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
{
case 'ezimage':
case 'ezbinaryfile':
case 'ezmedia':
{
$dataString = eZDir::path( array( $storageDir, $dataString ) );
break;
}
default:
}
$attribute->fromString( $dataString );
$attribute->store();
}

$operationResult = eZOperationHandler::execute( 'content', 'publish',
array( 'object_id' => $contentObjectID,
'version'=> 1 ) );

-----------------------------------

Well good luck with that anyway,

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.