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 Duan Camargo
Viktor (not verified)
Fri, 02/26/2021 - 15:34
Profile picture for user admin
admin
Fri, 02/26/2021 - 15:38

Hi Viktor, thanks a lot for great words !

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.