So let's say that I'm calling a node object, like such:

$node = node_load($nid);

Now, is there a method that lets me get a particular field value for that node, something like,


Or is there another way that it is preferred to do this?


Jaypan’s picture

Node objects do not contain methods, they only contain values.

You would want something along the lines of


Think the Drupal forums suck? Add your agreement here:

AndyBalloon’s picture

And would that be true for any field, for example, this?

Jaypan’s picture

And would that be true for any field, for example, this?


Usually. There are occasional exceptions. But if you dump the object, you can see its contents.

Think the Drupal forums suck? Add your agreement here:

Tobias Xy’s picture

Provided that you're using the Entity API module, it is possible to get the value via a method, but you have to wrap the node into an EntityDrupalWrapper:

$node = node_load($nid);
$wrapper = entity_metadata_wrapper('node', $node);
$value = $wrapper->body->value();

But Jaypan's method is probably faster and sufficient for your purposes. ;-)

AndyBalloon’s picture

Yes, my module requires the entity API (mainly because the module that it extends requires the entity API), but I'll try Jaypan's solution first if it is simpler.

Thank you both.

Tobias Xy’s picture

Not necessarily simpler (in my Opinion the Entity API method is simpler), but faster because you are working with the "raw" data instead of a wrapper object (I'm just guessing, though ;-) ).

In case you want to learn more about the Entity API way of handling the field data:

gausarts’s picture

You may also want to investigate:
With its params, you have more flexible way to render it as needed.

love, light n laughter

AndyBalloon’s picture

This doesn't seem to be returning any value for field_event_date. It works fine for title, and for the module in general. Am I missing something about naming conventions? field_event_date is the machine name for the field in question on the node.

$node = node_load($regid->entity_id);
$title = $node->title;
$date = $node->field_event_date[LANGUAGE_NONE][0]['value'];
Tobias Xy’s picture

Check which values are set:

exit; // in case you don't see anything only with the line above
AndyBalloon’s picture

I used print_r, do you think I should switch to var_dump?

Tobias Xy’s picture

It doesn't really matter, whether you are using print_r or var_dump, since their functionality is almost the same (only the output has a different format).
Try adding exit; after your print_r / var_dump, if you can't see any debugging output.

Jaypan’s picture

I have two functions I regularly use in development:

function p($item)
' . print_r($item, TRUE) . '
'); } function d($item) { die('
' . print_r($item, TRUE) . '
'); }

The first one dumps the object to a Drupal message, the second kills the process and dumps the item.

You may also want to install and enable the devel module, which will then give you access to a function, dpm(), that does the same as my p() function, but makes the dump collapsible, making it easier to see what you are looking at.

Think the Drupal forums suck? Add your agreement here:

dahousecat’s picture

If you use the devel module then check out the dmp function - is a bit like you p function but better...

I also have a function similar to your d function but allows you to pass multiple arguments into it and uses print_r or var_dump depending on the type of variable. Also passing a string of 'return' as one of the variables causes the process to carry on and not die.

function pdie() {
    $args = func_get_args();
	$style = 'style="background:#EEEEEE;padding:10px;margin:10px;border:1px solid #999999;"';
    foreach($args as $arg) {
    	if($arg!=='return') {
    		echo "<pre $style>".PHP_EOL;
    		if(in_array(gettype($arg), array('boolean', 'NULL'))) var_dump($arg);
    		else print_r($arg);
    		echo '</pre>'.PHP_EOL;
	if(!array_search('return', $args, true)) die();
Ayesh’s picture

Just use dargs().
It will pretty-print a list of arguments passed to the fiction that dargs() was called in.

// Ayesh
(Latest blog post: Drupal bad practices)