eZ Community » Blogs » Thiago Campos Viana » Tip: custom workflow operation triggers

By

Tip: custom workflow operation triggers

Friday 07 October 2011 7:02:41 pm

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

You may already know that ez comes with a bunch of default workflow operation triggers, like content->publish, in this post I will show how to create a custom one for your own modules, so users will be able to execute pieces of codes before/after certain module code (or eZ Publish cli script, cronjob, template operator, fetch function...), then they won't need to hack your module code for that, it will be necessary only create a separated event extension. 

Here's the steps:

Inside your module modules folder create the file operation_definition.php, with a content like:

<?php
 
$OperationList = array();
/* view is the name of your module operation, can have the same name of the view */
$OperationList['view'] = array( 'name' => 'view',
                                        'default_call_method' => array( 'include_file' => 'extension/yourmodule/classes/yourmoduleoperationcollection.php',
                                                                        'class' => 'YourModuleOperationCollection' ),
                                        'parameter_type' => 'standard',
                                        'parameters' => array( array( 'name' => 'param1',
                                                                      'type' => 'object',
                                                                      'required' => true ),
                                                               array( 'name' => 'param2',
                                                                      'type' => 'array',
                                                                      'required' => true )),
                                        'keys' => array( 'param1', 'param2' ),
 
                                        'body' => array( array( 'type' => 'trigger',
                                                                'name' => 'pre_view',
                                                                'keys' => array( 'param1',
                                                                                 'param2')
                                                                ),
                                                         array( 'type' => 'method',
                                                                'name' => 'view',
                                                                'frequency' => 'once',
                                                                'method' => 'helloView',
                                                                ),
                                                         array( 'type' => 'trigger',
                                                                'name' => 'post_view',
                                                                'keys' => array( 'param1',
                                                                                 'param2') ) )
                                        );
 
 
?>

Create a workflow.ini.append.php file inside your module settings folder, like this:

<?php /* 
 
[OperationSettings]
AvailableOperationList[]=yourmodule_view
 
 
*/?>

Now create a file named yourmoduleoperationcollection.php inside your module classes folder, something like this:

<?php
 
 
 
class YourModuleOperationCollection {
 
    static public function helloView($param1, $param2) {
        /* code goes here: echo 'hello operation?'; */
    }
 
}
 
?>

Then you'll need to call eZOperationHandler somewhere inside your view code (or eZ Publish cli script, cronjob, template operator, fetch function...), something like this:

...
    if (eZOperationHandler::operationIsAvailable( 'yourmodule_view' )) {
    
        eZOperationHandler::execute( 'yourmodule', 'view', array( 'param1' => 'a',
                                                               'param2' => 'b' ) );
    } else {
       YourModuleOperationCollection::helloView('a','b');
    } 
...

Finally your extensions users will be able to create events extensions for your new workflow trigger, the same way they create events for content->publish. They will need to specify your operation in trigger types list, like:

...
$this->setTriggerTypes(array('yourmodule' => array('view' => array('after')),'yourmodule' => array('view' => array('before'))));
...
Proudly Developed with from