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

drum5ormore’s picture

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).

puya’s picture

I Think you should be able to just put this in the body of a block with the inputformat set to php:

<h1 id='pagetitle' class='title'>
    <?php print drupal_get_title(); ?>
</h1>
RoyE’s picture

this worked perfectly for me. thanks

hectorplus’s picture

This was or is possible with Drupal 7, now i am searching the same thing but for Drupal 8! Any Ideas?

thanks

arh1’s picture

if you want to use your own PHP code, check out the node_load function.

kriskd’s picture

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?

mooffie’s picture

(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.

have the title of the current node be placed in a block instead of in the content area of the node

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'.

// Move the node title to the footer.
function _phptemplate_variables($hook, $vars) {
  if ($hook == 'node') {
    if ($vars['page']) { // Showing a single node on the page?
      // Yep.
      drupal_set_title(''); // clear the page title.
      drupal_set_content('footer', $vars['title']); // put the node title in the footer.
    }
  }
  return array();
}

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.

kriskd’s picture

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:

function lifeline_preprocess(&$vars, $hook) {
    if ($vars['page']) { // Showing a single node on the page?
      // Yep.
      drupal_set_title(''); // clear the page title.
      drupal_set_content('content_top', $vars['title']); // put the node title in the content_top.
    }

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?

mooffie’s picture

Yes, it's easy to put it in a block instead.

First, look at this line:

drupal_set_content('content_top', ...

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:

drupal_set_content('post_info', ...

(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:

<?php print drupal_get_content('post_info'); ?>

That's all.

(As for styling the block: first let me know if you got the step above.)

yuriy.babenko’s picture

In node.tpl.php:

global $node_title;
$node_title = $node->title;

in your block's content (PHP selected):

global $node_title;
print $node_title;

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

CleanCutRogue’s picture

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; ?>

crutch’s picture

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

tacoparty’s picture

Exactly what I needed. Thanks!

michaelmalak’s picture

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(); ?>

yuriy.babenko’s picture

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:

  echo db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => arg(1)))->fetchField();

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com