By raphaeltsr on
I need to create a dynamic block (through the administration page) that displays the node title $title.
I've tried simple possibilities such as print $title , print $block->subject ...
But none of them works because there is no $node or $block variables avaible to a block created through the adminstration page.
PS: Yes, I've selected the option PHP CODE.
Comments
Try using the views module
Try using the views module to create your block http://drupal.org/project/views. It will allow you to create a block with information about a node (including title).
I Think you should be able to
I Think you should be able to just put this in the body of a block with the inputformat set to php:
this worked perfectly for me.
this worked perfectly for me. thanks
This was or is possible with
This was or is possible with Drupal 7, now i am searching the same thing but for Drupal 8! Any Ideas?
thanks
if you want to use your own
if you want to use your own PHP code, check out the node_load function.
Interested as well
I'm also interested in how to do this. What I need is to have the title of the current node be placed in a block instead of in the content area of the node. I'm stumped as to how to accomplish this with views. With Views I can create a list of node titles, but how can I get it to display the title of the current node?
...
(I'm comming from here.) Your question is different than Raphael's. You also want to remove the title from its normal place. It can be done in the theming layer.
Here's how to move the title of the current node to the footer. (Moving it to a block is one more step.)
Put the following in your 'template.php'.
Unfortunately, you're probably going to have a _phptemplate_variables() there already, so you'll have to 'merge' the above into the existing: just take the
if ($hook == 'node') {}section. (Things are much easier in D6.)If it works, let us know.
Override phptemplate variable
Moofie,
Thanks for taking the time to walk me through this. I totally gotta learn this stuff.
I'm using the latest version of the zen theme in Drupal 5.7. Here is what I put in my template.php:
And it did work! My title is indeed in the content_top area.
Now as you mentioned, is it easy enough to get it into a block? You method puts it at the bottom of the content area and of course with a block, I could weight it appropriately and then also be able to style it a little easier. Also, maybe something easy, how would I get header tags around it?
...
Yes, it's easy to put it in a block instead.
First, look at this line:
It puts the node title in the 'content_top' region. Instead we're going to put it in a private region of our own, that isn't shown on the page; let's call it 'post_info'. So change that line to:
(No need to "define" this region anywhere!) The next step is to create a block that pulls in this region. Create a block, use 'PHP code' as its 'Input format' and in its body type:
That's all.
(As for styling the block: first let me know if you got the step above.)
In node.tpl.php: <?phpglobal
In node.tpl.php:
in your block's content (PHP selected):
Global variables for the win!
---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com
I think people are making this more complicated than necessary?
The following php code will tell you what the title is of the currently-viewed node:
<?php print db_result(db_query("SELECT title FROM {node} WHERE nid=".arg(1))); ?>but it's important to only show this block on a page that is a rendering of a valid node. You can ensure this by using the following php in the block's visibility setting:
<?php if (arg(0)=='node' && is_numeric(arg(1))) return true; ?>Result using above user
Result using above
user warning: Unknown column 'build' in 'where clause' query: SELECT title FROM node WHERE nid=build in C:\TWAMPd\htdocs\includes\common.inc(1685) : eval()'d code on line 1
Exactly what I needed.
Exactly what I needed. Thanks!
Drupal 7 tips
For Drupal 7, two notes:
1. Enable the PHP Filter module in Drupal Core as described in http://drupal.org/node/1046700
2. db_result is gone in Drupal 7 so the code needs to be
<?php print db_query("SELECT title FROM {node} WHERE nid=".arg(1))->fetchField(); ?>Do not use the above code, it
Do not use the above code, it is vulnerable to SQL injection attacks.
If you have to run that query (really not necessary), do it properly:
---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com