Lesson 5.4: A fetch function of your own
When a template needs data that no kernel fetch offers, or the same complicated fetch appears in many templates,
write a fetch function of your own. It needs three small files: a module, a description of the function, and a PHP
class that returns the data. In this lesson learners write fetch( 'hello', 'latest', ... ), which returns the
newest nodes of a class.
At a glance
| Course | Developers |
| Module | Module 5: Fetch functions |
| Duration | 60 minutes (20 presenting, 40 exercise) |
| Level | Advanced |
| Audience | Developers |
| Prerequisites | Lesson 5.3; mysite_hello is active (lesson 3.1) |
Learning objectives
After this lesson, learners can:
- write a minimal
module.phpso a module exists; - describe a fetch function in
function_definition.phpwithcall_methodandparameters; - write a function collection class whose method returns
array( 'result' => ... ); - register the module with
ExtensionRepositories[]andModuleList[]inmodule.ini; - regenerate the autoloads and call the fetch from a template.
Trainer's notes
How to present it
Sources: kernel/content/function_definition.php and a real extension, for example
extension/eztags/modules/tags/function_definition.php.
- Three files (4 minutes).
modules/hello/module.php(the module exists),modules/hello/function_definition.php(what the fetch is called and which parameters it takes), and a class inclasses/(what it does). - The description (5 minutes). Walk through the
$FunctionList['latest']block of the exercise:name,operation_types,call_methodwithclassandmethod,parameter_type => 'standard', and each parameter withname,type,requiredanddefault. Parameters are passed to the method in the order listed. - The class (4 minutes). The method returns
array( 'result' => $value ); the template receives$value. An error is returned asarray( 'error' => ... ), as the kernel's collections do. - Registration (4 minutes).
settings/module.ini.append.phpof the extension:[ModuleSettings] ExtensionRepositories[]=mysite_helloandModuleList[]=hello. Then the autoloads. - Where to look up nodes in PHP (3 minutes).
eZContentObjectTreeNode::subTreeByNodeID( $params, $nodeID )takesClassFilterType,ClassFilterArray,SortBy,Limit,OffsetandDepth, like the template fetch.
Common questions
Can a fetch function change data? It should not. Fetches are for reading; changes belong in a module view with a form and a permission check (module 6).
Does the fetch check permissions? subTreeByNodeID respects the current user's read rights by default, like
the content fetches.
Can I use the RAD tools? Yes: Setup > RAD, the template extension wizard (/setup/templateoperator)
writes template operators and fetch functions. Writing one by hand first makes the generated code readable.
Pitfalls
- Forgetting
'result'. Returning the array of nodes directly gives the template nothing useful. - Module not registered. Without
ModuleList[]andExtensionRepositories[]the fetch fails with an error about an unknown module in the debug output. - Class not found. Run
php bin/php/ezpgenerateautoloads.php -eafter adding the class.
Hands-on exercise
On the development installation, in extension/mysite_hello.
Create
modules/hello/module.php:$Module = array( 'name' => 'hello' ); $ViewList = array(); $FunctionList = array();Create
modules/hello/function_definition.php:$FunctionList = array(); $FunctionList['latest'] = array( 'name' => 'latest', 'operation_types' => array( 'read' ), 'call_method' => array( 'class' => 'mysiteHelloFunctionCollection', 'method' => 'fetchLatest' ), 'parameter_type' => 'standard', 'parameters' => array( array( 'name' => 'class_identifier', 'type' => 'string', 'required' => true ), array( 'name' => 'limit', 'type' => 'integer', 'required' => false, 'default' => 5 ) ) );Create
classes/mysitehellofunctioncollection.php:/** * Fetch functions of the hello module: {fetch( 'hello', 'latest', hash( 'class_identifier', 'article' ) )} */ class mysiteHelloFunctionCollection { /** * The newest published nodes of one class, newest first. * * @param string $classIdentifier * @param int $limit * @return array array( 'result' => eZContentObjectTreeNode[] ) */ public static function fetchLatest( $classIdentifier, $limit ) { $nodes = eZContentObjectTreeNode::subTreeByNodeID( array( 'ClassFilterType' => 'include', 'ClassFilterArray' => array( $classIdentifier ), 'SortBy' => array( 'published', false ), 'Limit' => (int)$limit ), 2 ); return array( 'result' => $nodes ? $nodes : array() ); } }Check all three with
php -l.Register the module:
php bin/php/console exp:ini add module.ini/ModuleSettings/ExtensionRepositories mysite_hello extension:mysite_hello php bin/php/console exp:ini add module.ini/ModuleSettings/ModuleList hello extension:mysite_hello php bin/php/ezpgenerateautoloads.php -e php bin/php/ezcache.php --clear-tag=iniIn the playground, call the fetch:
{foreach fetch( 'hello', 'latest', hash( 'class_identifier', 'folder', 'limit', 3 ) ) as $n}{$n.name|wash}; {/foreach}.Call it with
'note'and with a class that does not exist; compare.Leave out
class_identifierand run with--debug=warning,error; read the message about the required parameter.Commit the four files.
Expected result
- Step 6 prints up to three folder names, newest first.
- Step 7: the notes of lesson 4.1, and nothing for the unknown class.
- Step 8: the debug output shows the error "Missing parameter 'class_identifier' for function 'latest' in module 'hello'".
- The trainer checks one learner's
module.ini.append.phpand the playground output.
Quiz
- Which three files make a fetch function?
- What must the method of a function collection return?
A. The value itself B.
array( 'result' => $value )C.trueD. A template - In which order are parameters passed to the method?
- Which two settings register an extension module?
- Why should a fetch function not change data?
Answers
modules/<module>/module.php,modules/<module>/function_definition.php, and the class inclasses/.- B.
array( 'result' => $value ). - In the order the parameters are listed in
function_definition.php. module.ini [ModuleSettings] ExtensionRepositories[]andModuleList[].- Fetches run while pages are drawn, often from caches and without forms or permission checks for changes; changes belong in module views.
Further reading
- RAD tools: the template extension wizard.
- Extension points: fetch functions among the template extension points.
- Extension development: the book: Part 3 covers fetch functions in depth.
Related pages
- Module 5: Fetch functions
- Developers: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.