eZ Community » Learn » eZ Publish » Creating a simple custom workflow event

Creating a simple custom workflow event

Thursday 09 December 2010 5:01:18 pm

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

Step 6 - Develop a workflow event: posting a Twitter status

The idea here is to allow the editor to add a custom message that will be tweeted. For this we will need to edit any content class we want to add the Twitter Status Update feature. Our workflow event will check for the existence of a content class attribute to pick the message from. For the moment just check the code.

First we need to retrieve the object being published. This is where the $parameters array is useful for. Back to our twitterstatusupdatetype.php editing, continuation of the snippet above :

$objectID = $parameters['object_id']; 
$object = eZContentObject::fetch( $objectID );
$nodeID = $object->attribute( 'main_node_id' );
$node = eZContentObjectTreeNode::fetch( $nodeID );
$datamap = $object->dataMap();
 
// Now we need to check if the content class attribute “twitterstatus” 
// is present in the class and whether it is empty or not:
// Quit if attribute twitterstatus does not exist in the contentclass
if ( !isset( $datamap['twitterstatus'] ) ) 
{
        if ( $twitterDebugOutput == 'enabled' ) 
            eZLog::write("twitter status not found");
 
        return eZWorkflowType::STATUS_ACCEPTED;
}
if ( $twitterDebugOutput == 'enabled' )
    eZLog::write( "Found twitter status attribute" );
 
 
// Else take note of it
$twitterStatus = $datamap['twitterstatus']->attribute('data_text');
 
if( empty( $twitterStatus ) ) 
{
        if ( $twitterDebugOutput == 'enabled' )
            eZLog::write( "twitter status empty" );
 
        return eZWorkflowType::STATUS_ACCEPTED;
}
if ( $twitterDebugOutput == 'enabled' )
    eZLog::write( "twitter status attribute not empty" );
 

If everything is OK, we carry on and use TinyURL API to shrink the URL to our article. This is a quick way to do it, you could also use cURL. Please note that your site.ini.append.php file needs to be configured with the proper SiteURL pointing to the front end address of your website, you might need to also configure it in your admin siteaccess. This is not the best way to do this, so for a better code you might want to create a more complex code or maybe use another INI variable name so that SiteURL for your admin interface is the actual URL of the admin interface.

$siteINI = eZINI::instance();
$siteHost = $siteINI->variable( 'SiteSettings', 'SiteURL' );
$siteHost = preg_replace( "|/$|", "", $siteHost );
 
$tinyURL = eZHTTPTool::getDataByURL( 'http://tinyurl.com/api-create.php?url=' .  urlencode( "http://".$siteHost."/" . $node->attribute( 'url_alias' ) ), false, false );
 
if ( empty( $tinyURL ) ) 
{
    if ( $twitterDebugOutput == 'enabled' )
          eZLog::write("Error with TinyURL");
 
       return eZWorkflowType::STATUS_ACCEPTED;
}
 
try 
{
    $response = $twitter->updateStatus( "{$twitterStatus}: {$tinyURL}", 'xml' );
    if ( $response->isError() ) 
    {  
        if ( $twitterDebugOutput == 'enabled' )
            eZLog::write( "Twitter error: " . $response->http_code ); 
 
        return eZWorkflowType::STATUS_ACCEPTED;
    } 
    else 
    {
        if ( $twitterDebugOutput == 'enabled' )
            eZLog::write( "Twitter status updated: {$twitterStatus}: {$tinyURL}" );
    }
} 
catch( Arc90_Service_Twitter_Exception $e ) 
{
    if ( $twitterDebugOutput == 'enabled' )
        eZLog::write( "Error with Twitter API" );
 
    return eZWorkflowType::STATUS_ACCEPTED;
}
 
// After having sent the status update, we’ll clear it so it won’t be resent.
$datamap['twitterstatus']->setAttribute( 'data_text', '' );
$datamap['twitterstatus']->store();
if ( $twitterDebugOutput == 'enabled' ) 
    eZLog::write( "Resetting twitter status attribute" );
 

Printable

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

Author(s)

Proudly Developed with from