1

A user [content_editor] got deleted and the content created by this user is referring to anonymous user. While an administrator is editing that content, administrator is receiving user doesn't exist. The error goes off when we re-assign the content to administrator or an existing user. As the user has created paragraphs, nodes etc how do we track contents created and referred by the user as user id is already deleted?

How can we remove these references and save the content without error message.

I thought we can use Rules API to create a simple rule which will force save any entity if an admin is saving the content, then save the content. But the rules API is providing me only nodes and not entities which means paragraphs will still refer to old deleted user. How can I overcome this. Is there a programmatic solution to this problem?

1 Answer 1

1

supposing the content you want to reassign is of node type, here is how to do it programatically (in a hook_update_N() per example or /devel/php or drush ev...)

  $deleted_uid = 42;
  $reassigned_uid = 80;
  // Reassign nodes (current revisions).
  module_load_include('inc', 'node', 'node.admin');
  $nodes = \Drupal::entityQuery('node')
    ->condition('uid', $deleted_uid)
    ->execute();
  node_mass_update($nodes, ['uid' => $reassigned_uid], NULL, TRUE);
  // Reassign old revisions.
  db_update('node_field_revision')
    ->fields(['uid' => $reassigned_uid])
    ->condition('uid', $deleted_uid)
    ->execute();

hope this helps

1
  • Hi @izus, Thanks for the information, I will test this thoroughly and will get back to you on this. I have done a quick test on devel exec php on my local and it seems to work well, I also need to update paragraph that are referenced by each nodes and I managed to loop through the node and loaded referenced paragraphs using update them.
    – Regokonda
    Commented Dec 7, 2018 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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