eZ Community » Blogs » Thiago Campos Viana » Tip: Template fetch function to php

By

Tip: Template fetch function to php

Thursday 06 October 2011 8:07:26 pm

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

In this post I'll show how to use the equivalent of template fetch functions in php.

It's very simple, you can use the following static function:

eZFunctionHandler::execute( $moduleName, $functionName, $functionParameters );

So, if we have something like this in tpl language:

{def $home=fetch('content', 'node', hash('node_id',2) )}

It would become in php:

$home=eZFunctionHandler::execute('content', 'node', array('node_id'=>2));

If you want to navigate through the object attributes the same way you navigate in tpl language:

{$home.object.owner.id}

You can use the attribute function, this way:

$home->attribute('object')->attribute('owner')->attribute('id');

There's also a php attributes() funcion that returns an array with all object attributes. We need to remember, the equivalent of tpl {$home|attribute(show,1)} is var_dump($home) ;)

Note: If you are using a php script, some content needs you to login as admin first, like fetching users accounts, so you can do this before call the php fetch function:

$user = eZUser::fetchByName('admin');
$userID = $user->attribute( 'contentobject_id' );
eZUser::setCurrentlyLoggedInUser( $user, $userID );

There's a tutorial about how to develop your own fetch functions, then you will be able to use it in php the same way you use already existing ones, or in tpl language.

Proudly Developed with from