Tuesday 14 September 2010 4:41:06 am
As fetch functions must be part of a module, the only requirement is to have a declared module inside of an active extension.
The function_definition.php file will reside inside the module folder, but the function collection class doesn't need to be in it since it will be automatically loaded thanks to the built-in class autoload system. Best practices tell us to create a classes/ folder inside the extension and place our class here.
So called mymodule module need to be declared in extension/myextension/settings/module.ini.append.php :
<?php /* #?ini charset="utf-8"? [ModuleSettings] ExtensionRepositories[]=myextension ModuleList[]=mymodule */ ?>
And of course, be sure that your extension is activated in settings/override/site.ini.append.php (or in your siteaccess with ActiveAccessExtensions)
Et voilĂ ! You can now use your fetch function in templates :
{def $result = fetch( 'mymodule', 'fetch_function_name', hash( 'first_param', 'something', 'second_param', true() ) )} {$result.first_key} {* Will display "foo" *} {$result.second_key} {* Will display "bar" *}
One of the main advantages of fetch functions vs template operators is that they can easily be called in your PHP scripts. A handler class, eZFunctionHandler, with two shorthand methods are available for that :
<?php $myResult = eZFunctionHandler::execute( 'mymodule', 'fetch_function_name', array( 'first_param' => 'something_else', 'second_param' => true ) ); echo $myResult['first_key']; // Will display "foo" echo $myResult['second_key']; // Will display "bar"