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,

$node->getField('body')

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

Comments

Jaypan’s picture

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

You would want something along the lines of

$node->body[LANGUAGE_NONE][0]['value']
andy.alexander’s picture

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


$node->date[LANGUAGE_NONE][0]['value']
Jaypan’s picture

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

$node->date[LANGUAGE_NONE][0]['value']

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

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

andy.alexander’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:
https://drupal.org/node/1021556

gausarts’s picture

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

love, light n laughter

andy.alexander’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:

var_dump($node->field_event_date[LANGUAGE_NONE][0]);
exit; // in case you don't see anything only with the line above
andy.alexander’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)
{
  drupal_set_message('<pre>' . print_r($item, TRUE) . '</pre>');
}

function d($item)
{
  die('<pre>' . print_r($item, TRUE) . '</pre>');
}

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.

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.