Hello, I see many implementations of hook_block who declare global $user but none with global $node I tried it but $node is still not set, so I cannot access the node object directly. the solution would be to do like in book_hook() and check on arg(): if (arg(0) == 'node' && is_numeric(arg(1))) but it looks like a hack and for what I have in mind, it makes things more complicated. Isn't there a possibility to have directly access to the $node object? How? thanks, Augustin. -- http://www.wechange.org/ Because we and the world need to change. http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
On 7/1/06, Augustin (a.k.a. Beginner) <drupal.beginner@wechange.org> wrote:
I see many implementations of hook_block who declare global $user but none with global $node
I tried it but $node is still not set, so I cannot access the node object directly.
That is because there is no global $node. There is a global $user viewing the page, but not every page has a $node that it needs to display (eg all kinds of "listing" pages or the admin pages etc). Blocks can appear on any page, not only node-pages. So hook_block() doesn't have direct access to a $node variable. Note that the functions that need $node always have a $node parameter passed. There is no global $node that I'm aware of (with maybe the exception of theming, but that is then handled by phptemplate). the solution would be to do like in book_hook() and check on arg():
if (arg(0) == 'node' && is_numeric(arg(1))) but it looks like a hack and for what I have in mind, it makes things more complicated.
I think this is the correct way of doing this. And I don't really see how this makes things more complicated. The test checks if we are on a node-page, and if you are you can load the node needed. node.module makes sure the node is loaded only once, so there is no overload either. Isn't there a possibility to have directly access to the $node object?
How?
Don't think so. Kind regards, Robrecht
On Saturday 01 July 2006 05:50 pm, Robrecht Jacques wrote:
Don't think so.
Kind regards, Robrecht
Thank you Jacques for your clear answer. I'll do with arg(), then :) Blessings, Augustin. -- http://www.wechange.org/ Because we and the world need to change. http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
Op zaterdag 1 juli 2006 09:54, schreef Augustin (a.k.a. Beginner):
if (arg(0) == 'node' && is_numeric(arg(1))) but it looks like a hack and for what I have in mind, it makes things more complicated.
This is the current way of doing that. And since that specific line is repeated all over Drupal and its contribs, I just added it to helpers.module: /** * Find out if the current page is a node page. * @return TRUE if the page is a node, FALSE if it is another page. */ function is_node_page() { if (arg(0) == 'node' && is_numeric(arg(1))) { return TRUE; } return FALSE; } Bèr
On Monday 03 July 2006 04:49 pm, Bèr Kessels wrote:
This is the current way of doing that. And since that specific line is repeated all over Drupal and its contribs, I just added it to helpers.module:
what and where is the helpers.module? thanks, augustin. -- http://www.wechange.org/ Because we and the world need to change. http://www.reuniting.info/ Intimate Relationships, peace and harmony in the couple.
Op maandag 3 juli 2006 16:43, schreef Augustin (a.k.a. Beginner):
what and where is the helpers.module?
http://drupal.org/node/62453 « Helpers is a library, or toolbox, with lots of very useful functions that help you build your modules and sites with joy.»
Wouldn't it be better to also return arg(1), making it a mixed return value? The node id (evaluates to true) on sucess, FALSE on failure. Since it already does the check, it is just logical to return the nid. Goba On Mon, 3 Jul 2006, [iso-8859-1] Bèr Kessels wrote:
This is the current way of doing that. And since that specific line is repeated all over Drupal and its contribs, I just added it to helpers.module:
/** * Find out if the current page is a node page. * @return TRUE if the page is a node, FALSE if it is another page. */ function is_node_page() { if (arg(0) == 'node' && is_numeric(arg(1))) { return TRUE; } return FALSE; }
Bèr
Op dinsdag 4 juli 2006 15:54, schreef Gabor Hojtsy:
Wouldn't it be better to also return arg(1), making it a mixed return value? The node id (evaluates to true) on sucess, FALSE on failure. Since it already does the check, it is just logical to return the nid.
Maybe, yes. I am not a fan of mixed return types. I know PHP is rather liberal about that but I too often found myself cornered because I did a foreach on some return value, only to find out that some ugly coded (IMO) function returns a FALSE of an ARRAY(). ugh. I think a set of such helper modules might be a better plan. I named this function is_node_page (*is*) on purpose, it is a test. in addition something like find_page_node_id() or even get_page_node() etc could be other handy modules. Bèr
On Tue, 4 Jul 2006, [iso-8859-15] Bèr Kessels wrote:
Op dinsdag 4 juli 2006 15:54, schreef Gabor Hojtsy:
Wouldn't it be better to also return arg(1), making it a mixed return value? The node id (evaluates to true) on sucess, FALSE on failure. Since it already does the check, it is just logical to return the nid.
Maybe, yes. I am not a fan of mixed return types. I know PHP is rather liberal about that but I too often found myself cornered because I did a foreach on some return value, only to find out that some ugly coded (IMO) function returns a FALSE of an ARRAY(). ugh.
I think a set of such helper modules might be a better plan. I named this function is_node_page (*is*) on purpose, it is a test. in addition something like find_page_node_id() or even get_page_node() etc could be other handy modules.
Right, keeping the name and changing the behaviour to return the $nid would mislead developers. Goba
participants (4)
-
Augustin (a.k.a. Beginner) -
Bèr Kessels -
Gabor Hojtsy -
Robrecht Jacques