Issue:

Created a custom content entity. This entity will have data programmatically created for it by the application, and as such, calls the simple create method of the entityTypeManager and then calls the save method on the resulting created entity.

Here's the code I use where '....entity....' is the id of the content entity:

$new= \Drupal::entityTypeManager()->getStorage('...entity...')->create($values);
$new->save();

Upon saving, the site successfully saves the content entity into the database with all of the appropriate values, however, it then trows this error:

Recoverable fatal error: Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given, called in ...\lib\Drupal\Core\Routing\RequestContext.php on line 31 and defined in Drupal\Core\Routing\RequestContext->fromRequest() (line 37 of core\lib\Drupal\Core\Routing\RequestContext.php).

Drupal\Core\Routing\RequestContext->fromRequest(NULL) (Line: 31)
Drupal\Core\Routing\RequestContext->fromRequestStack(Object)
call_user_func_array(Array, Array) (Line: 332)
Drupal\Component\DependencyInjection\Container->createService(Array, 'router.request_context') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('router.request_context', 3) (Line: 494)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Object) (Line: 329)
Drupal\Component\DependencyInjection\Container->createService(Array, 'private__b7bcd0b1968d44ae44212040b5b5ae5a1185e77f') (Line: 509)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Object) (Line: 236)
Drupal\Component\DependencyInjection\Container->createService(Array, 'url_generator') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('url_generator', 1) (Line: 494)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Object) (Line: 236)
Drupal\Component\DependencyInjection\Container->createService(Array, 'comment.manager') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('comment.manager') (Line: 158)
Drupal::service('comment.manager') (Line: 377)
comment_entity_insert(Object)
call_user_func_array('comment_entity_insert', Array) (Line: 402)
Drupal\Core\Extension\ModuleHandler->invokeAll('entity_insert', Array) (Line: 169)
Drupal\Core\Entity\EntityStorageBase->invokeHook('insert', Object) (Line: 418)
Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('insert', Object) (Line: 470)
Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, ) (Line: 304)
Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, ) (Line: 395)
Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 761)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 364)
Drupal\Core\Entity\Entity->save() (Line: 188)

This is happening in the save method. When you look at the trace, it all starts in the post-entity save module invocation and lands in the comment module.

I've tried the exact same save routine with the example module's content entity example to be sure it's not just my custom code with the exact same results. It succeeds on the save, but fails in the post-save invocation.

If I put a return in the function comment_entity_insert, the save works perfectly each time.

Is this a D8 core comment issue or a piece of functionality that needs to be documented to disable the comments?

Comments

_randy’s picture

A resolution to this issue:

The entity creation routines, while they do create data entries in the database, and even though Drupal is bootstrapped enough to get services to fire during the post-entity-save routine, those services may ask to get current URL context. If you're not in a fully bootstrapped environment (a controller servicing a route is fully bootstrapped), the call will fail. Even though the entity saved just fine, it's the post-save calls that may fail.

The resolution is to ensure you're in a high-enough bootstrapped environment.

CB’s picture

I apologise for replying to such an old comment, but I wanted to let you know that I had this issue - I was definitely executing code before full bootstrap and didn't realise that was the problem until I saw this. Thanks! Hopefully any others with this issue will see this.