$node->nid not working in a block?
I'm trying to set up a block to display an image of my choosing in a node just about the node title. As I really can't figure out any other way to do it, I settled on a custom block region and a php coded block to do it. Here's what I did so far...
Created a custom block region in template.php:
'abovecontent' => t('abovecontent'),
Added this region where I want it in page.tpl.php:
<?php
print $abovecontent;
?>Created a php block and assigned it to display in the abovecontent region:
<img src="files/node<?php print $node->nid ?>.jpg">
What I want to do there is have it spit out an image link that corresponds to the node name. That way I can create a new node and if there's a corresponding image named the same it will display it. I'll just be uploading the image through the Image Module and naming it to the node that I want it to display on.
The content shows up right where I want to if I insert some text to test it. The img code also shows up right where I want it, but it echoes:
<img src="files/node.jpg">
The $node->nid doesn't spit out the node id.
If I put this same code right onto the node page, say create a new book page and put that line at the top of the content section, it displays correctly with the node id.
Does anyone have any thoughts? I'm sure this is some roundabout way to go about doing this, but I'm quite the Drupal/PHP lightweight and know just enough to be dangerous...
Thanks for the help.
Tim

$node not set in blocks
There is no concept of the current node in a block. If you are talking about displaying this block when the page is displaying a single node you can do something like this
if ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {$node = node_load(arg(1));
// Do something with the node
}
The 'if' will be true when displaying a node but not when editing a node. If you want to block to show when editing a node also remove "&& ! arg(2)".
Does arg(1) work all the
Does arg(1) work all the time in getting the node id ?
I'm in fact trying to use $node->nid in the main node pages but don't seem able to get it to work. Do I need to load something first or global some variable ? (tried global $node but it doesn't help). Or is arg(1) the only straightforward way to get the current node nid ?
The availablity of $node depends on context
The variable $node is available in page.tpl.php and node.tpl.php. For node.tpl.php $node is always available, for page.tpl.php only when viewing a single node on a page.
arg(1) is not always the nid of a node, you need to check arg(0) and make sure it is 'node' before using arg(1) as a nid.
Thanks. But I was referring
Thanks. But I was referring to accessing $node inside the body of a node itself, i.e. the user input body. Example, I would create a "page" node with PHP input format like this :
<?php
global $node;
print 'This is node : ' . $node->nid;
?>
The result would just display "This is node :" without showing the actual node ID value. Did I miss out something ?
There is no global $node and nodes don't know themselves
There is no global $node and nodes don't know themselves at least within the body. That is inside the body there is no variable that represents the node the body belongs to.
node_load() perhaps?
If you have a way to get the id of the node, you should be able to get it via node_load.
<?php
$nid = 123; // however you get the id
$node = node_load($nid);
print $node->title; // etc
?>
Yep, it's getting nid that
Yep, it's getting nid that I'm wondering about. There're several indirect ways of getting the node id, but no straightforward way from the content body. I guess I'll stick with arg().
There is no concept of the
Is there some way to change this? I am desperately in need of something like that.
You need to define what current node means
Blocks appear on pages, there may or may not be one or more nodes on that page. For example, if you view node/1 you are viewing a page which shows the contents of a single node. Note though there could be blocks that also contain nodes. If you view taxonomy/term/1 it will list all content with the taxonomy id, so the page is made up of zero or more nodes. Many/most admin pages are not nodes at all.
So the question is, how do you define current node?
Well, it is of the node/1
Well, it is of the node/1 variety, so that would be a page showing the contents of a single node. I really need a block in the sidebar to be able to tell what date the node was created on.
Maybe somethiing like this
This snippet will print the create date for the current node being viewed (if there is one)
<?phpif ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
$node = node_load(arg(1));
print format_date($node->created);
}
?>
If you do not want to limit it to when the node is viewed (for exampled edited), remove
&& ! arg(2)from the if.THANK YOU! That put me on
THANK YOU! That put me on the right track and got it solved.
Path aliases
Will this method work when the page is called with an alias?
--Arancaytar
Yes, arg(0), arg(1) ... arg(n) hold the unaliased path name
Yes, arg(0), arg(1) ... arg(n) hold the unaliased path name so even if you alias the path for a node arg(0) is still 'node', etc.
Thanks, this helps me as
Thanks, this helps me as well!
Thanks! This provided part
Thanks! This provided part of the solution for a code snippet I was trying to write in this thread:
http://drupal.org/node/141458
Thanks again!
Drupal's so easy, even I could do it.
http://www.kfol.org/
You can save resources by
You can save resources by prevent loading node again if replace
<?php$node = node_load(arg(1));
?>
to
<?phpstatic $nodes; $node=&$nodes[arg(1)];
?>
This is getted from node_load function.
That will not work
The code you reference is local to node_load() and calling node_load() with just a nid (as per your example) already prevents the node from being loaded more than once. Since there is no global $nodes your code does not really do anything (static is not the same as global). In general it is best to stick to using the api's Drupal provides.
maybe nodeasblock module can help
not tested but i'm using it for other needs
:)
url aliases - use seesion variables
if you use any kind of aliases in you url, then you can't get node id from arg(1),
i had a block appear on every node of a certan type so i needed to know stuff about it.
so i put
<?php $_SESSION[nodeid_is]=$node->nid; ?>in my node.tpl.php at the top;
and
<?php $nid = $_SESSION[nodeid_is]; ?>in my block, you can then load the node from the nid.
Obviously the seesion variable get replacesd with the differnt nid everytime you visit a diffent node / page / etc. Also, it is retained, so you should remove it using unset(); if you need to.
Actually arg() returns the parts of the unaliased path
Actually arg() returns the parts of the unaliased path so if you are viewing a single node arg(0) will be 'node' and arg(1) the node id (nid).
Note that your solution places the nid of the last node viewed. So it will even set the value when viewing a list of nodes (for example the default front page or a list produced by views). It will also reset it for any blocks that produce a node view, in fact it will be called anytime node_view() is called and as such is not guarenteed to produce the same results as using arg(0) and arg(1).
Similar solution...
I was having a similar problem and ended up using the $_SESSION variable as above. It was executing too late in node.tpl.php, making the second node on the page display the subnodes that belonged to the first node on the page. So I ended up putting it in views.module, modifying theme_views_view_nodes as follows...
<?php
function theme_views_view_nodes($view, $nodes, $type, $teasers = false, $links = true) {
foreach ($nodes as $n) {
$node = node_load($n->nid);
// added session variable
$_SESSION['nodeid_is']=$node->nid;
$output .= node_view($node, $teasers, false, $links);
// unset session variable
unset($_SESSION['nodeid_is']);
}
return $output;
}
?>
The page loads all "artist_profile" nodes, and uses Insert view to show the view "artist_store". "artist_store" shows all art nodes linked to artist_profile by nodereference. The code above makes $node-nid from artist_profile available to "artist_store" so that it can select the artists using the argument code field with the following:
<?phpif (isset($_SESSION['nodeid_is'])) {
$args[0] = $_SESSION['nodeid_is'];
}
else {
$args[0]=arg(1); }
return $args;
?>
and the nodereference field as an argument in the view.
Hi, Is this a solution to
Hi,
Is this a solution to get the $args inside a node-body?
I would like to use the arguments in the url, inside the node-title and node-body as a sort of template working.
Thanks in advance for your reply!
greetings,
Martijn