By ParisLiakos on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x
Introduced in version:
8.x
Issue links:
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
How to boot the Kernel with language support?
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?
Call to undefined function Drupal\language\language_get_default_langcode()
Workaround, for now...
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.
Article need chages
Error: Call to undefined method Drupal\Core\DrupalKernel::handlePageCache()The code above didn't work
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
An alternative, simpler, approach here would be to just:
This also works, but it has the downside of printing out the full HTML of the homepage every time your external script is executed.
That seems to work too for me, except..
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