Share icon

Are you looking for how to load node and node fields using nid to into your custom work, then this article is helps you to achieveing this.

Node is just entity in drupal and in drupal 8 entity is loading new way using function "entityTypeManager()"

entityTypeManager()

Below is the example how to load the node using nid

$nid = 22; 
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$node = $node_storage->load($nid);

you can choose your nid "node_storage" load the $node object it is same "node_load()" function in drupal 7

So using this $node you can get the title and body and any custom fields you want

Get node's NID:

$node->id();

Get node's content type:

$node->bundle();

Other Fields like body and your custom fields:

$node->get('title')->value;  // title field
$node->get('created')->value;   // node created timestamp
$node->get('body')->value;   // "The full node body.

Images fields and fiile id

$node->get('field_image')->target_id;

using same way we can get fields connected to that node.

Comments

Profile picture for user genawellish6412
Viktor (not verified)
17 Jan 2022
This has been incredibly helpful, I've implemented it several times. All your tutorials have been quite helpful by the way, thank you so much!
Profile picture for user admin
admin
17 Jan 2022

Hi Viktor, thanks a lot for great words !

Add new comment

Restricted HTML