How to load node by id in Drupal 8?

by wilmer.lemke , in category: PHP Frameworks , 8 months ago

How to load node by id in Drupal 8?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by lew , 3 months ago


In Drupal 8, you can load a node by its ID using the Node::load() method. Here is an example of how you can use this method:

$node = Drupal

Where $nid is the ID of the node you want to load.

Alternatively, you can use the entity_load() function, which allows you to load multiple nodes at once:

$nodes = Drupal::entityTypeManager()->getStorage('node')->loadMultiple([$nid1, $nid2, ...]);

Here, $nid1, $nid2, etc. are the IDs of the nodes you want to load. The function returns an array of nodes, indexed by their IDs.

Note that both of these methods will return a fully-populated node object, including all field values and other properties. If you only need a limited set of data for the node, you may want to consider using a custom query to retrieve the data from the database.