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

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)

Importing Images and Files

Images and files are added by using in the name of your file so they quite straightforward. The only addition is that you need to make sure to specify the storage directory for the file so that eZ knows where to look. If you have issues with a missing image then the problem is most likely going to be there (if you are running the script from the command line make sure your debug is turned on). The following code is based on the previous example. Again, check the CMS admin panel to make sure the image has been created:

// 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( 'media/images/imported_images' );
$cli->output( 'Parent: ' . $parent_node->attribute( 'name' ) );
 
//setting node details
$params = array();
$params['class_identifier'] = 'image';
$params['creator_id'] = $user->attribute( 'contentobject_id' ); //using the user extracted above
$params['parent_node_id'] = $parent_node->attribute( 'node_id' ); //pulling the node id out of the parent 
$params ['storage_dir' ] = $_SERVER['PWD'] . '/var/ezflow_site/storage/import_images/';
/*
required so ez knows where to look. The ending "/" required. 
$_SERVER['pwd'] is being used as the script is being run through 
the command line. I’ve created the folder “import_images” on 
the server and moved my image into it.
*/
 
//setting attribute values
$attributesData = array ( ) ;
$attributesData['name'] = 'Adding a random image'; 
$attributesData ['image'] = 'my_image.jpg'; 
 
//storing xml content for the caption
$XMLContent = "<p>David's Picture Test</p>";
$parser = new eZSimplifiedXMLInputParser();
$parser->setParseLineBreaks( true );
$document = $parser->process( $XMLContent );
$xmlString = eZXMLTextType::domString( $document );
$attributesData['caption'] = $xmlString;
 
$params['attributes'] = $attributesData;
 
//publishing node
$imageObject = eZContentFunctions::createAndPublishObject( $params );

Most of this should be familiar to before but these are the lines to look out for:

Code :

$params['storage_dir' ] = $_SERVER['PWD'] . '/var/ezflow_site/storage/import_images/';

This tells eZ where to look, we are running the script from the command line, otherwise you would need to change $_SERVER['PWD']. Also note the trailing “/” which is necessary. In this instance, I’ve created a folder called import_images which is stored within the var directory.

Code :

$attributesData ['image'] = 'my_image.jpg' ;

And this is our image. We are also storing a description on this object which utilises the XML information we used previously. Once we run the script we should now have a new image accessible through the CMS:

Imported image

 

Printable

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

Author(s)

Proudly Developed with from