I try to get a php command line job running, actually testing it currently by calling it via an url for convenience.

There are all kinds of source code examples for reading a node from drupal, all end with the same problem when calling the nodes save method.

I can read the node without any problem, but when storing the updated value i.e. title it throws:

PHP Fatal error: Call to undefined function Drupal\\Core\\Entity\\Plugin\\DataType\\entity_load() in /var/www/mysite/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php on line 70

define('DRUPAL_DIR', '/var/www/mysite/');

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\node\Entity\Node;

$autoloader = require_once DRUPAL_DIR . 'autoload.php';

$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();

define('TESTID', 12);

$storage = \Drupal::entityManager()->getStorage('node');
$node = $storage->load(TESTID);
$fTitle = $node->get('title');
$sTitle = $fTitle->value;
echo $sTitle;
$fTitle->value = 'New Title';
$node->save();

Comments

kkretsch created an issue. See original summary.

kkretsch’s picture

Component: other » base system
dawehner’s picture

In order to fully load drupal you also need to execute $kernel->preHandle() :(

kkretsch’s picture

Thanks, that did it:

$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$kernel->boot();
$kernel->preHandle($request);

Now I can save that node. Case closed!

kkretsch’s picture

Status: Active » Closed (fixed)