Forums / Developer / Get current node within an operator

"Please Note:
  • At the specific request of Ibexa we are changing this projects name to "Exponential" or "Exponential (CMS)" effective as of August, 11th 2025.
  • This project is not associated with the original eZ Publish software or its original developer, eZ Systems or Ibexa".

Get current node within an operator

Author Message

Jérôme Vieilledent

Thursday 26 June 2008 1:57:26 am

Hello,

I would like to know if it was possible to get the current viewed node in a template operator, such as we do with the $node variable in a template.
I guess this variable is registered somewhere and accessible with PHP code but how ?

Thanks by advance

André R.

Thursday 26 June 2008 7:40:38 am

Look in the first parameter in your modify function on the template operator class ( $tpl ).

Example code (if in pagelayout):

                    if ( $tpl->hasVariable('module_result') )
                    {
                       $moduleResult = $tpl->variable('module_result');
                    }

                    if ( $tpl->hasVariable('current_node_id') )
                    {
                       $currentNodeId = (int) $tpl->variable('current_node_id');
                    }
                    else if ( isset( $moduleResult['node_id'] ) )
                    {
                       $currentNodeId = (int) $moduleResult['node_id'];
                    }
                    else if ( isset( $moduleResult['path'][count( $moduleResult['path'] ) - 1]['node_id'] ) )
                    {
                       $currentNodeId = (int) $moduleResult['path'][count( $moduleResult['path'] ) - 1]['node_id'];
                    }

if in node template just use the $node variable.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Jérôme Vieilledent

Thursday 26 June 2008 7:54:42 am

Thanks André !

So we can access to the current node with this :

print_r($tpl->Variables['']['node']);

André R.

Thursday 26 June 2008 7:57:41 am

To print the current node id:

$node = $tpl->variable('node');
print_r( $node->attribute('node_id') );

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Jérôme Vieilledent

Thursday 26 June 2008 8:29:33 am

You're right, that's much cleaner :-D

yunxia cai

Tuesday 20 April 2010 7:34:12 pm

Hello!

  I have a question.

 first:I define the {def $current_node= fetch( content, node, hash( node_id, $current_node_id ) ) }

 then if the class is not folder ,I need define the $current_node

if I define again  {def $current_node= fetch( 'conten't, 'list', hash( 'parent_node_id', $node.parent_node_id ) ) }

and ,the  $current.node.module_result.content_info.object_id is right?

Thanks!