How to load a Node in Drupal 9

Node object can be accessed in two ways,
By loading the node id.
By using node parameters.

Include the Node class

use Drupal\node\Entity\Node;

To get node object using Node Id,

$nid = 'Node Id';

$node = Node::load($nid);

To get node object using node parameters,

$node = \Drupal::routeMatch()->getParameter('node');

Note: Use the above method, when it's a node page.

Category