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.

eZ debug

Timing: Jan 30 2025 21:46:04
Script start
Timing: Jan 30 2025 21:46:04
Module start 'layout'
Timing: Jan 30 2025 21:46:04
Module start 'content'
Timing: Jan 30 2025 21:46:04
Module end 'content'
Timing: Jan 30 2025 21:46:04
Script end

Main resources:

Total runtime0.0239 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0057 588.1406151.2109
Module start 'layout' 0.00570.0041 739.3516220.7031
Module start 'content' 0.00980.0126 960.05471,001.7891
Module end 'content' 0.02240.0015 1,961.843833.9922
Script end 0.0239  1,995.8359 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002611.0180140.0002
Check MTime0.00114.5682140.0001
Mysql Total
Database connection0.00083.358610.0008
Mysqli_queries0.00249.999930.0008
Looping result0.00000.050910.0000
Template Total0.00114.510.0011
Template load0.00093.625810.0009
Template processing0.00020.882510.0002
Override
Cache load0.00062.492010.0006
General
dbfile0.00031.159780.0000
String conversion0.00000.030940.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs