I am trying to remove a user programmatically with the following simple code:

$group = \Drupal\group\Entity\Group::load($gid);
$user = \Drupal\user\Entity\User::load($uid);
$group->removeMember($user);

But I get this error: TypeError: Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given

Not sure if it is a bug or just me who is doing it wrong?

Comments

Sleeepy created an issue. See original summary.

Sleeepy’s picture

Issue summary: View changes
Sleeepy’s picture

Full error:

|1613737301|php||||0||TypeError: Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given, called in /home/www/core/lib/Drupal/Core/Routing/RequestContext.php on line 31 in Drupal\Core\Routing\RequestContext->fromRequest() (line 37 of /home/www/core/lib/Drupal/Core/Routing/RequestContext.php) #0 /home/www/core/lib/Drupal/Core/Routing/RequestContext.php(31): Drupal\Core\Routing\RequestContext->fromRequest(NULL)#012#1 [internal function]: Drupal\Core\Routing\RequestContext->fromRequestStack(Object(Symfony\Component\HttpFoundation\RequestStack))#012#2 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(276): call_user_func_array(Array, Array)#012#3 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\Component\DependencyInjection\Container->createService(Array, 'router.request_...')#012#4 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(434): Drupal\Component\DependencyInjection\Container->get('router.request_...', 3)#012#5 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(273): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)#012#6 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(449): Drupal\Component\DependencyInjection\Container->createService(Array, 'private__zpo0hT...')#012#7 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(237): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)#012#8 /home/www/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\Component\DependencyInjection\Container->createService(Array, 'url_generator')#012#9 /home/www/core/lib/Drupal.php(552): Drupal\Component\DependencyInjection\Container->get('url_generator')#012#10 /home/www/core/lib/Drupal/Core/Url.php(854): Drupal::urlGenerator()#012#11 /home/www/core/lib/Drupal/Core/Url.php(799): Drupal\Core\Url->urlGenerator()#012#12 /home/www/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php(62): Drupal\Core\Url->getInternalPath()#012#13 /home/www/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(881): Drupal\path\Plugin\Field\FieldType\PathFieldItemList->delete()#012#14 /home/www/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(743): Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod('delete', Object(Drupal\group\Entity\GroupContent))#012#15 /home/www/core/lib/Drupal/Core/Entity/EntityStorageBase.php(428): Drupal\Core\Entity\ContentEntityStorageBase->doDelete(Array)#012#16 /home/www/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(786): Drupal\Core\Entity\EntityStorageBase->delete(Array)#012#17 /home/www/core/lib/Drupal/Core/Entity/EntityBase.php(403): Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete(Array)#012#18 /home/www/modules/group/src/Entity/Group.php(182): Drupal\Core\Entity\EntityBase->delete()#012#19 /home/www/sites/app.site.dk/modules/custom/site_hidetip/site_hidetip.module(28): Drupal\group\Entity\Group->removeMember(Object(Drupal\user\Entity\User))#012#20 /home/www/core/lib/Drupal/Core/Extension/Extension.php(147): include_once('/home/www/sites...')#012#21 /home/www/core/lib/Drupal/Core/Extension/ModuleHandler.php(128): Drupal\Core\Extension\Extension->load()#012#22 /home/www/core/lib/Drupal/Core/Extension/ModuleHandler.php(141): Drupal\Core\Extension\ModuleHandler->load('site_hideti...')#012#23 /home/www/core/lib/Drupal/Core/DrupalKernel.php(586): Drupal\Core\Extension\ModuleHandler->loadAll()#012#24 /home/www/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(45): Drupal\Core\DrupalKernel->preHandle(Object(Symfony\Component\HttpFoundation\Request))#012#25 /home/www/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#26 /home/www/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#27 /home/www/vendor/asm89/stack-cors/src/Asm89/Stack/Cors.php(49): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#28 /home/www/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Asm89\Stack\Cors->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#29 /home/www/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#30 /home/www/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#31 /home/www/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)#012#32 /home/www/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))#012#33 {main}.