This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit

eZ Community » Learn » eZ Publish » Creating eZ Publish Objects in PHP

Creating eZ Publish Objects in PHP

Friday 04 June 2010 5:09:02 pm

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 3 - Adding the Content (continued)

Simple Content – A Folder (continued)

Retrieving background information

Code :

// getting information required to setup the node:
$user = eZUser::fetchByName( 'Import' ); // import our user (replace with the code for the previous step if necessary)
if ( !$user )
    //if no user exists let's pull out the current user:
    $user = eZUser::currentUser();
$cli->output( 'Username: ' . $user->attribute( 'login' ) );
$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'my_imported_stuff' );
$cli->output( 'Parent: '. $parent_node->attribute( 'name' ) );
We will use this to tell eZ where our new node will be stored, 
note the underscores rather than hyphens and that it is all 
in lowercase.

The first section of our script just works out who should be importing the content and where they should be importing it to. We’ve covered the user code but the next line is equally important as it extracts the node details for the node we want our new folder to sit under:

Code :

$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'my_imported_stuff' );

The fetchByURLPath is perfect for this, there are a couple of things to be aware of with the function:

  • The fetchByURLPath uses underscores instead of hyphens to replace characters such as whitespace
  • The path is all in lower case
  • To retrieve a node from a different directory separate the directories with a forward slash (as you would expect). For example:

Code :

$parent_node = eZContentObjectTreeNode::fetchByURLPath( 'media/images' );

Preparing general object information

Code :

//setting general node details
$params = array();
$params['class_identifier'] = 'folder'; //class name (found within setup=>classes in the admin if you need it
$params['creator_id'] = $user->attribute( 'contentobject_id' ); //using the user created above
$params['parent_node_id'] = $parent_node->attribute( 'node_id' ); //pulling the node id out of the parent
$params['section_id'] = $parent_node->attribute( 'object' )->attribute( 'section_id' );
we don't need to do this as the section will default to that 
of the parent but if you want to use a different node for the 
section the same approach can be used, just pull out the node 
with the section you are using and then pull the SectionID from it.

In this section we start storing the information eZ Publish requires to create the node. We will pass eZ Publish the $params array when we tell it to create the folder. First we tell it the type of object we will be creating and then we pull out the user id and the node id that we established in the first part of the script.

The section_id is stored only for completeness and your example will work fine without it. If you need your node to be off a different section to it’s parent, you can either use the eZSection::fetchFilteredList method to pull out the section by name, or if you want to use a section of a specific node, use eZContentObjectTreeNode::fetchByURLPath as we have done above and use the path to that node. The section_id can then be extracted using the same code as we have used above.



Printer Friendly version of the full article on one page with plain styles


Proudly Developed with from