Community asked.. Please clarify your specific problem...
Fair comment. What I'd like help with is how do I correct the script below so it works with D9.
<?php
use Drupal\Core\DrupalKernel;
$autoloader = require_once 'autoload.php';
// This next line is the problem
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
require_once 'core/includes/database.inc';
require_once 'core/includes/schema.inc';
$em = $kernel->getContainer()->get('entity_type.manager');
$entity = $em->getStorage('node')->create(array(
'type' => 'article',
'title' => 'Druplicon test',
));
$entity->save();
?>
This is the same as the D8 script below which works - except I have:
- removed the use symphony line;
- replaced the deprecated function ( thanks leymannx )
The script fails as noted on the line
$request = Request::createFromGlobals();
with messages
chrome-error://chromewebdata/:1 Failed to load resource: the server responded with a status of 500 ()
VM10:7288 crbug/1173575, non-JS module files deprecated.
Can someone point out what's wrong with this line in D9 (which I guess relates to the removal of the symphony use statement) and also if there are any other lines of code following which D9 won't like.
Thanks for any further help on this and apologies for the earlier confusion and for still being out of my depth with D9.
Original post...
This article provides D8 code to create an entity from an external script. I'm in the same boat as the person who asked this question - wanting to convert from D7's drupal_bootstrap.
Although my requirement is D9, I thought I'd try and get a D8 script working using the above post. His application is with an entity. I'm wanting to create article entries.
Here is the code I took from the above link which is said to work..
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
require_once 'core/includes/database.inc';
require_once 'core/includes/schema.inc';
$em = $kernel->getContainer()->get('entity.manager');
$entity = $em->getStorage('node')->create(array(
'type' => 'article',
'title' => 'Druplicon test',
));
$entity->save();
?>
This code runs and creates an entry in the node_field_data table but the article is not listed as content on the site.
I'm very much feeling my way with D8/D9 coding so please bear with me if I'm doing something silly!
Once I can get this to work, I then need to find out what changes are needed for D9 - so any help here would be good. For example the D8 code uses symphony but that's not part of D9 I believe.
Thanks for any help on this to get this to work. And to help me replace my D7 experience with D8/D9!
Sorry... This code does show up in the content - I was looking at a D9 site, not the test D8 I set up. Apologies!
But I'd still like help on what changes I need to make to this script to run under D9.