Template fetch function
Note: For an extensive tutorial on how to make custom fetch functions, please read Understanding and developing fetch functions.
We now know how to transmit parameters and GET / POST variables to a new view and show them. But if we want to show data (for example our database table) in an eZ Publish template, we cannot handle it just by opening the view.
To do this we use the fetch functions from the "kernel" eZ module, for example {fetch('content', 'node', hash( 'node_id', 2 )}. We want to define two fetch functions, list and count, which are opened by the following template syntax:
{fetch( 'modul1', 'list', hash( 'as_object' , true() ) )} {fetch( 'modul1', 'count', hash() )}
The function list shows the data entries as arrays or objects (based on the setting of the parameter ‘as_object'). The count function doesn't have any parameters and uses an SQL command to determine the amount of data in our database table jacextension_data.
The definition of the fetch functions is done in the file jacextension/modules/modul1/function_definition.php. This also defines which parameters are transmitted to which PHP function within the specified PHP class.
<?php $FunctionList = array(); // {fetch('modul1','list', hash('as_object', true()))|attribute(show)} $FunctionList['list'] = array( 'name' => 'list', 'operation_types' => array( 'read' ), 'call_method' => array( 'class' => 'eZModul1FunctionCollection', 'method' => 'fetchJacExtensionDataList' ), 'parameter_type' => 'standard', 'parameters' => array( array( 'name' => 'as_object', 'type' => 'integer', 'required' => true ) ) ); //{fetch('modul1','count', hash())} $FunctionList['count'] = array( 'name' => 'count', 'operation_types' => array( 'read' ), 'call_method' => array( 'class' => 'eZModul1FunctionCollection', 'method' => 'fetchJacExtensionDataListCount' ), 'parameter_type' => 'standard', 'parameters' => array() ); ?>
Listing 16. Definition of the fetch functions of modul1 - extension/jacextension/modules/modul1/function_defintion.php
The file jacextension/modules/modul1/ezmodul1functioncollection.php has a help class that contains all the fetch functions.
<?php class eZModul1FunctionCollection { public function __construct() { // ... } /* * Is opened by('modul1', 'list', hash('as_object', $bool ) ) fetch * @param bool $asObject */ public static function fetchJacExtensionDataList( $asObject ) { return array( 'result' => JACExtensionData::fetchList( $asObject ) ); } /* * Is opened by('modul1', 'count', hash() ) fetch */ public static function fetchJacExtensionDataListCount() { return array( 'result' => JACExtensionData::getListCount() ); } } ?>
Listing 17. Help class with PHP functions that are used in the Template Fetch definition in function_defintion.php –
extension/jacextension/modules/modul1/ezmodul1functioncollection.php
Hint: In each module, the file functioncollection.php lists the possible parameters for the hash() part of a fetch function. Investigate the file kernel/content/ezcontentfunctioncollection.php to determine which parameters are available for the fetch function {fetch('content', 'tree', hash( ... ) )} of the kernel content module of eZ Publish. This helps if the eZ documentation is incomplete.
And of course, as for every new PHP class, do not forget to regenerate autoloads.