I am trying to create Drupal entity instances from an external script that is not part of Drupal or a Drupal module.

For Drupal 7 there was drupal_bootstrap to load the required Drupal dependencies. However, after a long search all I found for Drupal 8 was a note that drupal_bootstrap is deprecated for Drupal 8.

And indeed, I get a

Call to undefined function drupal_bootstrap()

when I try to call it after including as in

define('DRUPAL_ROOT', __DIR__ .'/../drupal');
require_once DRUPAL_ROOT . '/core/vendor/autoload.php';
require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';

So how can I use the Drupal 8 API from an outside script?!

  • You can use drush php-script. This execute php code with a full Drupal bootstrap directly from a shell script. – rpayanm Sep 15 '15 at 19:27

For current version of drupal 8.x:

define('DRUPAL_DIR', '/usr/share/nginx/html');
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
require_once DRUPAL_DIR . '/core/includes/database.inc';
require_once DRUPAL_DIR . '/core/includes/schema.inc';
// Specify relative path to the drupal root.
$autoloader = require_once DRUPAL_DIR . '/autoload.php';

$request = Request::createFromGlobals();

// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');

$em = $kernel->getContainer()->get('entity.manager');

$entity = $em->getStorage('node')->create(
          'type' => "article",
          'title'=> "test entity",
          'body' => "body body body",

  • You don't need require_once and preparelegacyrequest. Here's some suggestions. // Avoid errors if you use realpath chdir(DRUPAL_DIR); // Uncomment if you need a processed request object. // $kernel->prepareLegacyRequest($request); // $kernel->handle($request); – Teenage Oct 20 '20 at 16:55

I finally ended up with this code. It has some ugly extra includes but at least it works:

define('DRUPAL_DIR', __DIR__ .'/../drupal');

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');

require_once DRUPAL_DIR . '/core/includes/database.inc';
require_once DRUPAL_DIR . '/core/includes/schema.inc';

$em = $kernel->getContainer()->get('entity.manager');

$entity = $em->getStorage('my_entity')->create(array(
        'id' => "116",
        'name' => "test entity",
  • You can get by without all those require_once calls if you just require autoload.php: require_once 'your_drupal/autoload.php'; – paul-m Sep 19 '15 at 19:46
  • I think I do require autoload.php (line 4)? This did suffice when I used handleRequest but not when trying to boot only the basics to get to the entity manager. – sleidig Oct 1 '15 at 9:42
  • 2
    You may need to add chdir() – Parag Feb 26 '17 at 3:38
  • Removing require_once gives "Call to undefined function Drupal\Core\Entity\Sql\drupal_schema_get_field_value()" which is quite lame :( – Clemens Tolboom Jan 13 '20 at 13:54
  • 1
    You don't need require_once. Here's some suggestions. ``` lang-php // Avoid errors if you use realpath chdir(DRUPAL_DIR); // Uncomment if you need a processed request object. // $kernel->prepareLegacyRequest($request); `// $kernel->handle($request);``` – Teenage Oct 20 '20 at 17:01

Firstly: Always look for change notices. You can find them here: https://www.drupal.org/list-changes

Here's the main one dealing with drupal_bootstrap(): https://www.drupal.org/node/2275139

Have a look at Drupal 8's front controller (index.php).

You make an instance of the kernel and then have it handle requests.

But since you only want to poke entities into it, you won't handle requests. You just need to bootstrap the kernel and then use the various services to add your entities. The list of services is in core/core.services.yml and is quite huge. You might look for entity_manager.

Also, D8 has a RESTful API built in, so you might just be able to let D8 do all the work while you ping it over http.


For bootstrapping Drupal 8 from an external script, I would use the following code, similar to the code used in the authorize.php file.

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

// Change the directory to the Drupal root.
chdir('the Drupal root directory');
$autoloader = (require_once 'autoload.php');

try {
  $request = Request::createFromGlobals();
  $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');

  // Ensure the request includes the session if appropriate.
  if (PHP_SAPI !== 'cli') {
  // Your code.

} catch (HttpExceptionInterface $e) {
  $response = new Response('', $e->getStatusCode());

The database.inc file contains only deprecated functions, and it should not be used. The schema.inc file isn't necessary to run queries on entities.

To be able to query node entities, the code needs to load the System and the Node modules.

\Drupal::moduleHandler()->addModule('system', 'core/modules/system');
\Drupal::moduleHandler()->addModule('node', 'core/modules/node');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.