Share » Learn » eZ Publish » Understanding and developing fetch...

Understanding and developing fetch functions

Tuesday 14 September 2010 4:41:06 am

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

Developing a custom fetch function

Inside a module

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.

Example for "mymodule" module

Example for "mymodule" module

 

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" *}
 

Using it in PHP

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 :

  1. eZFunctionHandler::execute() - same as fetch in templates
  2. eZFunctionHandler::executeAlias() - same as fetch_alias in templates
<?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"
 
36 542 Users on board!

Tutorial menu

Printable

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

Author(s)