0

I'm using Drupal's php filter to add php code in node body of some of my nodes. Among other things, I'm also including a php file. That php file is doing some non-Drupal work and it is included in those nodes properly. However, I can't print Drupal's fields in that file.

Here is an example of php code in node's body field:

<?php

some work is happening here

include 'non-drupal.php'; // include non-drupal php file

some work is happening here

?>

Here is an example of non-drupal.php:

<?php

echo date('h:i:s'); // This works

print $title; // This doesn't work

?>

When I open that node, I can see the time, but there's no node title displayed.

Is it possible to print node fields from "external" php files?

EDIT (code added from dropbucket) I have tried with adding this code from Dropbucket, but it's not working either):

<?php

echo date('h:i:s'); // This works

function get_node_title($nid) {
return db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
} // This doesn't work (There is no output, but no errors displayed either)

?>
  • What is your goal with that file? – Huelfe Sep 21 '15 at 11:12
  • It's doing some non-Drupal work, but in some cases I need to use Drupal's fields in it to make it work properly. – take2 Sep 21 '15 at 11:23
  • Either you could overwrite node.tpl.php, create your own module or use drupal_bootstrap. – Huelfe Sep 21 '15 at 11:30
2

You could try it with drupal_bootstrap, but it is not tested.

define('DRUPAL_ROOT', '/path/to/drupal');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$title = drupal_get_title();
echo $title;
0

Have you tried to used GLOBALS ? or drupal_get_title() ?

EDIT : Can you try instead :

$title = drupal_get_title()
// use your variable $title
  • I'm not sure what should I do with GLOBALS, but get_title might be what I'm after. What code exactly should I use? I have tried the code at dropbucket.org/node/756, but it's not working either. – take2 Sep 21 '15 at 11:30
  • @take2 What code did you try? What did it do to make you think it won't work? – Clive Sep 21 '15 at 11:58
  • @Clive I have tried the code at dropbucket that I have linked to (function get_node_title...). It didn't output node title. – take2 Sep 21 '15 at 11:59
  • If your code's not here, it's invisible (makes sure we don't have questions that rely on code in a 3rd party site that will inevitably be removed at some point). Please edit the question and provide full details of where you're stuck. I think I can guess, but no sense wasting time if I'm wrong – Clive Sep 21 '15 at 12:01
  • @clive I have updated the question. – take2 Sep 21 '15 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.