I want to check, in a node template file (node-node-type.tpl.php), that code is executing because of a direct request from a user visiting a node page or from calling node_load function in somewhere else?


the problem was 'Number of posts on main page' option in 'Post settings'. its value was 30 (I don't remember why I set it to its maximum value but I'm sure there was a reason that wasn't related to it's true functionality I think it triggers something else).

now that we know problem is from 'Number of posts on main page' option how I can I fix it? why this option invokes node template file despite the fact that I use a custom page.tpl.php which doesn't call node_view at all?

  • Can you please elaborate your question? Thanks – ninjascorner Jun 21 '12 at 7:23
  • node_load is a core function no node can be loaded without this call. – niksmac Jun 21 '12 at 7:30
  • 1
    @ninjascorner a node template file executes: 1- when a user see that specific node type(=content type) page e.g. node/nid 2-when there is a call to node_load(nid).Now in node-node-type.tpl.php how can I check that? – sepehr Jun 21 '12 at 7:33
  • 1
    Please, explain WHY. Maybe we can find better solution :) – kalabro Jun 21 '12 at 7:46
  • 3
    @sepehr node_load() doesn't ever invoke the node template file. It's a function for loading a node, it has zero connection to the theme layer. I think you're getting it confused with node_view(), which does invoke the node template file – Clive Jun 21 '12 at 7:57

Edit: I realise after writing the answer that I sound rather negative. Sorry about that, but I can't phrase this in a positive fashion.

Answer: First, and foremost, you are mixing logic, with output. The template should not have logic in it, and should absolutely not care about why it was called.

When a user visits node/1, core will call node_load(1). This is in no way different from calling node_load(1) anywhere else.

A node template does also not execute as a direct result of loading a node. Templates are used when the theme layer renders a node, which is the completely other end of the response.

You can, in theory, call debug_backtrace() to get the call stack, and from there figure out more information about from where node_load was called, but there is almost certainly exactly 0 valid use-cases for this. I guarantee that you do not want to go down that path, and even considering it means that something is seriously wrong.

I have built Drupal-sites professionaly for years now, and have done some really, really ugly hacks during that time. Neither of them would come anywhere near analysing the stacktrace, in the theme layer.

| improve this answer | |
  • 1
    For debug and exploration purposes Devel's ddebug_backtrace() can be used too. – kalabro Jun 21 '12 at 7:44

node will displayed after creating renderable array via node_view(node_load($nid)). so if there is somewhere where you render node yourself, you can pass extra variable to node_view and catch it in node--node-type.tpl.php

another solution is using context module, where you can set static variable and get it in node--node-type.tpl.php then unset it again

context_set('mynamespace', 'customrender', true);

print drupal_render(node_view(node_load($nid))); //and in node--node-type.tpl.php context_get('mynamespace', 'customrender');

context_set('mynamespace', 'customrender', false);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.