Something basic in Drupal 8 and Drupal 9 (which are almost the same) when we’re programming is to get (load) a node by its ID. In Drupal 7, this used to be done like this:
1$nid = 123; // example value2$node = node_load($nid);
Now in Drupal 8, everything seems a bit more complicated at first, and there are several ways to do it:
1$nid = 123; // example value2$node_storage = \Drupal::entityTypeManager()->getStorage('node');3$node = $node_storage->load($nid);
Or in a single-line version:
1$nid = 123; // example value2$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
Or in the version that looks very similar to Drupal 7:
1$nid = 123; // example value2$node = \Drupal\node\Entity\Node::load($nid);
And if you’re going to do this multiple times, it’s better to declare the namespace with use so you can write less code. Do this at the top of the PHP file where you’ll use Node later.
1use Drupal\node\Entity\Node;23$nid = 123; // example value4$node = Node::load($nid);