Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By andy.alexander on
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
Node objects do not contain
Node objects do not contain methods, they only contain values.
You would want something along the lines of
And that would be true for
And would that be true for any field, for example, this?
And would that be true for
Usually. There are occasional exceptions. But if you dump the object, you can see its contents.
Provided that you're using
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:
But Jaypan's method is probably faster and sufficient for your purposes. ;-)
Yes, my module requires the
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.
Not necessarily simpler (in
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
You may also want to
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
This doesn't seem to be
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.
Check which values are
Check which values are set:
I used print_r, do you think
I used print_r, do you think I should switch to var_dump?
It doesn't really matter,
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.
I have two functions I
I have two functions I regularly use in development:
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.
DPM
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.
Just use dargs().
Just use dargs().
It will pretty-print a list of arguments passed to the fiction that dargs() was called in.
What's new and changing in PHP 8