0

I've turned on the PHP filter so I can put PHP in block text, however the following variables:

<?php print $node_url; ?>
<?php print $title ?>
<?php print $directory; ?>

Cause the following errors:

Notice: Undefined variable: node_url in eval() (line 2 of /modules/php/php.module(80) : eval()'d code).
Notice: Undefined variable: directory in eval() (line 2 of /modules/php/php.module(80) : eval()'d code).
Notice: Undefined variable: title in eval() (line 3 of /modules/php/php.module(80) : eval()'d code).

I know this worked in Drupal 6. Any suggestions?

--Marshall

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105

3 Answers3

2

The answer is that in a Drupal 7 theme, the node object does not necessarily exist. Go figure. So you have to declare it yourself and create your own variables. And when operating on it, you have to do so within an 'if isset()' statement. So to generate the variables you do this:

<?php
$directory = drupal_get_path('theme', 'THEME_NAME');
$node = menu_get_object();
if (isset($node)) {
    $nid = $node->nid;
    $node_url = 'node/' . $nid;
    $title =  $node->title;
    ...[rest of code goes here]
    ...[can't use variables derived from $node outside the 'if isset()']
}

?>

1

About your variable << $directory >>, I suppose that you mean to get node alias: If you get already the NID, you cat get it in the follow way:

$path = drupal_get_path_alias('node/' . $node->nid);
hugronaphor
  • 948
  • 8
  • 23
0

The block content is created before the theme layer goes into action. This mean that at the time the block is built, those variables aren't available. You will have to load the node yourself and create the variables you need.

Marius Ilie
  • 3,283
  • 2
  • 25
  • 27