I am trying to access a node programmatically and getting errors though using (AFAIK) the recommended way.

My code (so far) is really simple.

use Drupal\node\Entity\Node;

$node = Node::load($nid);
print_r($node, true);

It keeps failing with this error message.

[09-Nov-2020 08:32:31 UTC] PHP Fatal error: Uncaught Error: Class 'Drupal\node\Entity\Node' not found in test-node-access.php

The program resides (at least currently) in Drupal root directory. I am acting on user action via AJAX call from a paragraph (as a lame explanation to a little un-orthodox implementation).

  • 2
    For bootstrapping Drupal for an external script: How can I bootstrap? (2nd example) (this is NOT the recommended method for Ajax requests within Drupal itself!)
    – Hudri
    Nov 9, 2020 at 8:48
  • 1
    Yes, definitely not recommend. Instead of a single PHP file define a route pointing at a controller which returns a response in the format the AJAX call is expecting.
    – 4uk4
    Nov 9, 2020 at 9:22
  • Yep, I will do it with a proper controller
    – hank
    Nov 9, 2020 at 10:11


Browse other questions tagged or ask your own question.