We already saw in another article how to load a node by its ID, but now we need to know how to get the values of its fields.
It’s very simple — you just need to know that the node object has the get method, which allows us to retrieve these fields (attributes).
It’s important to note that these fields are also objects themselves, each with their own attributes, depending on the field type. You can find out which attributes are available by debugging with Xdebug (recommended) or by printing the variable using Kint or dump().
Using the get() method
echo $node->get('title')->value; // "Lorem Ipsum..."
echo $node->get('created')->value; // 1510948801
echo $node->get('body')->value; // "The full body, <strong>with HTML</strong>"
echo $node->get('body')->summary; // "The summary"
// A text field
echo $node->get('field_foo')->value; // "Whatever is in your custom field"
// A file field
echo $node->get('field_image')->target_id; // 432 (The File ID)
Using magic methods (shorthand)
With PHP magic methods, you don’t even need to write get. So the equivalent of the above would look like this:
echo $node->title->value; // "Lorem Ipsum..."
echo $node->created->value; // 1510948801
echo $node->body->value; // "The full body, <strong>with HTML</strong>"
echo $node->body->summary; // "The summary"
echo $node->field_foo->value; // "Whatever is in your custom field"
echo $node->field_image->target_id; // 432
Both approaches work exactly the same. Use whichever you prefer.
Common field types and their properties
| Field type | How to get the value |
|---|---|
| Text / String | ->value |
| Text with summary | ->value, ->summary, ->format |
| Integer / Float | ->value |
| Boolean | ->value (returns 0 or 1) |
| Date | ->value (ISO format string) |
| Entity reference | ->target_id, ->entity |
| Link | ->uri, ->title |
| File / Image | ->target_id, ->alt, ->title, ->entity |
Working with entity references
When you have a reference field (like a Media, Term, or User reference), you can get either the ID or the full entity:
// Get just the ID
$media_id = $node->get('field_image')->target_id;
// Get the full referenced entity
$media = $node->get('field_image')->entity;
$file_url = $media->get('field_media_image')->entity->getFileUri();
Checking if a field has a value
Always check before accessing to avoid errors:
if (!$node->get('field_foo')->isEmpty()) {
$value = $node->get('field_foo')->value;
}
Multi-value fields
For fields that can have multiple values:
// Get all values
foreach ($node->get('field_tags') as $item) {
echo $item->target_id;
}
// Get first value only
$first = $node->get('field_tags')->first();
// Get as array
$values = $node->get('field_tags')->getValue();
getValue() and getString()
Two very useful methods that work on any field:
// getValue() - Returns the raw array structure
$node->get('field_foo')->getValue();
// Returns: [['value' => 'my text']]
// For a multi-value field:
// Returns: [['value' => 'first'], ['value' => 'second']]
// getString() - Returns a plain string representation
$node->get('field_foo')->getString();
// Returns: "my text"
// For entity references, getString() returns the ID
$node->get('field_tags')->getString();
// Returns: "15" (or "15, 23, 42" for multi-value)
getValue() is useful when you need the complete data structure, especially for complex fields or when passing data to an API.
getString() is handy for quick debugging or when you just need a simple string output.
Helper methods for common fields
Some base fields have dedicated getter methods:
$node->getTitle(); // Same as $node->title->value
$node->getCreatedTime(); // Same as $node->created->value
$node->getOwnerId(); // Same as $node->uid->target_id
$node->isPublished(); // Returns boolean
$node->bundle(); // Returns the content type machine name
These methods are cleaner and recommended when available.
What is modern Drupal?
When we say “modern Drupal” we refer to Drupal 10, 11 and future versions. These versions share the same entity API and field system, so everything explained in this article applies to all of them.
Drupal 7 and earlier used a completely different approach. Drupal 8 and 9 introduced the current system, but they are no longer supported. That's why we focus on Drupal 10+ as "modern Drupal".