Skip to content

Instantly share code, notes, and snippets.

@logii
Created May 9, 2016 19:59
Show Gist options
  • Save logii/8a406d3abc41e884adaacc6dd3362856 to your computer and use it in GitHub Desktop.
Save logii/8a406d3abc41e884adaacc6dd3362856 to your computer and use it in GitHub Desktop.
Create A Node Programmatically on Drupal 8
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
// $response->send();
use Drupal\node\Entity\Node;
use \Drupal\file\Entity\File;
// Create node object with attached file.
$node = Node::create([
'type' => 'article',
'title' => 'Druplicon test',
// 'field_image' => [
// 'target_id' => $file->id(),
// 'alt' => 'Hello world',
// 'title' => 'Goodbye world'
// ],
]);
$node->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment