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
|