Change record status: 
Project: 
Introduced in branch: 
8.x
Introduced in version: 
8.x
Description: 

D7

// Retrieve the site path
$site_path = conf_path();
// Bootstrap drupal to different levels
drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
drupal_bootstrap(DRUPAL_BOOTSTRAP_KERNEL);
drupal_bootstrap(DRUPAL_BOOTSTRAP_PAGE_CACHE);
drupal_bootstrap(DRUPAL_BOOTSTRAP_CODE);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

D8

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

// Specify relative path to the drupal root.
$autoloader = require_once __DIR__ . '/../../autoload.php';
$request = Request::createFromGlobals();
// Retrieve the site path
$site_path = DrupalKernel::findSitePath($request);
// Bootstrap drupal to different levels
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();

$kernel->prepareLegacyRequest($request);

Note: In D8, instead of using conf_path(), use \Drupal\Core\DrupalKernel::getSitePath() instead. If the kernel is unavailable (such as above, where the code shows the booting of the D8 kernel) or the site path needs to be recalculated then Drupal\Core\DrupalKernel::findSitePath() can be used.

Impacts: 
Module developers

Comments

Todd Young’s picture

When booting a multilingual D8 site via script I can't seem to access the system's default language during a Node::create() - but I have entityQuery, Node::load() and such. Any tips?

$node = \Drupal\node\Entity\Node::create(array('type' => 'story', 'title' => 'Test Title'));

Call to undefined function Drupal\language\language_get_default_langcode()

Todd Young’s picture

Until I can figure out how to bootstrap D8 with default langcode available, I worked around the problem by specifying 'langcode' => 'en' in Node::create which is fine - for as long as I can make that assumption.

serg_admin’s picture

Error: Call to undefined method Drupal\Core\DrupalKernel::handlePageCache()

MKorostoff’s picture

The code above didn't work for me. I was able to boot Drupal from an external script using this code, which I pretty much just copied from index.php

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

define('DRUPAL_DIR', '../../../../../docroot');
$autoloader = require_once DRUPAL_DIR . '/autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
chdir(DRUPAL_DIR);
$response = $kernel->handle($request);
$kernel->terminate($request, $response);

//My custom code here can now access the entire Drupal API

An alternative, simpler, approach here would be to just:

require_once '../../../../../docroot/index.php';

This also works, but it has the downside of printing out the full HTML of the homepage every time your external script is executed.

rferguson’s picture

I can't seem to load some of the helper functions like so:

var_dump(function_exists('node_save'));

It gives me false.

My bad, it works with node_load, etc. It turns out there is no node_save function in 8.x