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

Creating eZ Publish Objects in PHP

Friday 04 June 2010 8:09:02 am

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

Step 3 - Adding the Content

We’ll now start importing our content. Although the basic process is straightforward, there are a couple of things to look out for so we’ll look at importing different content in turn. eZ Publish makes use of the fromString methods when carrying out the import. If the object you are creating contains fields of types we are not covering here, I’d recommend checking out the fromString documentation found here.

For the examples below we will not be using any hard coded node ids. These result in massive complications when you are moving your files between your development, staging and live environments and so I would advise avoiding them whenever possible. It also makes the code much simpler to understand and easier to follow when you do not use them.

Simple Content – A Folder

We’ll start off by importing the most simple thing we can: a folder. We will ignore the description for now and concentrate on getting the object into eZ Publish. I’ve created a folder called “My Imported Stuff” in the Content Structure Home folder of the CMS so we will import content into there. Don’t worry about the complexity of it. We will break it down after we have used the script.

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.
*/

//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.
*/

//setting attribute values
$attributesData = array ( ) ;
$attributesData['name'] = 'A brand new folder' ; 
$attributesData ['short_name'] = 'Shorter name' ; 
$params['attributes'] = $attributesData;

print_r( $params ); //lets print out the data so we know exactly what is being stored

//publishing the content:
$contentObject = eZContentFunctions::createAndPublishObject( $params );

if ( $contentObject )
{
    $cli->output( '===========================' );
    $cli->output( 'Output:' );
    $cli->output( "Content Object ID: " . $contentObject->attribute( 'id' ) );
    $cli->output( "Name: ".  $contentObject->attribute( 'name' ) );
    $cli->output( "Data Map: " . print_r( $contentObject->attribute( 'data_map' ), true ) );
}

You should now have a script you can use! Make sure you replace the user details you are using and also replace the name of the folder you are saving into if necessary. We can split this script up into four parts:

 

Printable

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

Author(s)