Modify and Publish Object with PHP

Author Message

Erik Weinmaster

Friday 25 June 2010 10:29:53 am

I've found the function createAndPublishObject which works great if you are creating objects. But what about editing them? I've looked on this site and found some references to using something along the lines of:

$contentObjectAttribute->fromString($newValue);

$contentObjectAttribute->store();

However, this doesn't follow the eZ Publish 'Publishing' process. What id does is changes the attribute of the object. What it doesn't do is increment the version, and thus keep track of the history. I am trying to write another function to do this, and below is what I have so far.

static function modifyAndPublishObject( $contentObject, $contentAttributes = null )
{
if(is_object($contentObject))
{
$db = eZDB::instance();
$db->begin();
$currentVersion = $contentObject->currentVersion();
$nextVersion = eZContentObjectVersion::create($contentObject->ID, false, $currentVersion->attribute('version') + 1);
$nextVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$nextVersion->store();
if (is_array($contentAttributes) && count($contentAttributes) > 0)
{
$attributes = $contentObject->attribute( 'contentobject_attributes' );
foreach( $attributes as $attribute )
{
$attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' );
if ( isset( $contentAttributes[$attributeIdentifier] ) )
{
$dataString = $contentAttributes[$attributeIdentifier];
switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
{
case 'ezimage':
case 'ezbinaryfile':
case 'ezmedia':
{
$dataString = $storageDir . $dataString;
break;
}
default:
}
$attribute->fromString( $dataString );
$attribute->store();
}
}
}
$db->commit();
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
'version' => $nextVersion->attribute('version') ) );
}
else
{
eZDebug::writeError( "Parameter contentObject not actual object.", 'CCContentFunctions::modifyAndPublishObject' );
}
return $contentObject;
}

This almost works. Unfortunately, what happens is that the new version of the object gets created, but BOTH the new version and previous version get the changes. How can I write this so that ONLY the new version has the changes?

Thanks,

-erik

Vincent Tabary

Friday 25 June 2010 11:43:09 am

Hi Erik !

You should look at this function :

eZContentFunctions::updateAndPublishObject()

I think it is what you are looking for.

About your function, you have updated the attributes from the $contentObject. You have to update the attributes directly from the $nextVersion

Look at this source on line 308

Vinz
http://vincent.tabary.me

Erik Weinmaster

Monday 28 June 2010 7:23:19 am

Thanks! I updated the kernel file eZContentFunctions.php from 4.2 to 4.3 and used the updateAndPublishObject. Works great.

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