1

Change node author works :

$node = \Drupal\node\Entity\Node::load($nid);
$node->set('uid','45');
$node->save();

But i don't know how to set it to anonymous user ? or unset it ?

 $node = \Drupal\node\Entity\Node::load($nid);
 // set uid to anonymous ?
 $node->save();
0
1

$node->set('uid', 0); $node->save should have worked. Maybe you forgot to flush caches.

To programmatically set the author to anonymous user on any entity you can call $entitiy->setOwnerId(0); and then $entity->save();, done.

$entity->setOwnerId(0);
$entity->save();
2
  • i figured out $node->set('uid', 0); $node->save worked indeed but the content was not listed on /admin/content because of the view relation with author. When opting out "Exiger cette relation" ,the content is there but the user column is empty instead of anonymous – Matoeil Apr 26 at 15:11
  • both options work then but the content author is not set to anonymous as it is when deleting content and reassiging using the backoffice. The consequence is the /admin/content view must be modified in order to see the reassigned content with ownerId 0 – Matoeil Apr 26 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.