1

It's possible to bootstrap Drupal 8 and create a new node using answer # 174474.

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');
$kernel->boot();
$kernel->prepareLegacyRequest($request);
$em = $kernel->getContainer()->get('entity.manager');

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

Is it possible to bootstrap a subsite database when using multisite to create the new node in a specific subsite?

In Drupal 7, this can be done by using the function drupal_override_server_variables, but this function doesn't exist in Drupal 8.

I also tried modifying the $_SERVER['HTTP_HOST'] and $_SERVER['SCRIPT_NAME'] variables according to this forum post, with no luck.

1

I was able to get this working by setting the server variable $_SERVER['SCRIPT_NAME'] to '/subsite/index.php' (where /subsite/ is the url path to the subsite).

$_SERVER['SCRIPT_NAME'] = '/subsite/index.php';

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');
$kernel->boot();
$kernel->prepareLegacyRequest($request);
$em = $kernel->getContainer()->get('entity.manager');

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

$entity->save();
| improve this answer | |

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.