4

This function function node_assign_owner_action() is provided by the Drupal 7 API to allow modules change node ownership,

I've searched, but not found a similar function that let me change the ownership of comments. Does anyone know how to change ownership of a comment without hacking the {comment} table directly, and without breaking existing incoming links?

I know that I can update the fields {comment}.uid, {comment}.name and {comment}.mail with the appropriate values, but as I understand things, contributed modules should never update tables "owned" by another module directly.

The only method I've found so far is to recreate the comment under new ownership. This is not a good solutions because it will invalidate all incoming links that used the permalink associated with the original comment.

2
  • One check this:drupal.org/files/issues/drush_extras.933030.patch also, however it is doing the same as you have mentioned. Commented Oct 14, 2013 at 7:25
  • comment.api.php simply dies not have a function to do it. I wonder if it could be handled ore generally, with entity-related functions.
    – Mołot
    Commented Oct 14, 2013 at 7:50

1 Answer 1

7

This changes the comment author details using the core comment API:

$cid = 12; // e.g get from db_select
$comment = comment_load($cid);
$comment->uid = 1;
$comment->name = 'admin';
$comment->mail = '[email protected]';
comment_save($comment);

You could also do something similar with a Rules component for use with VBO.

2
  • 1
    Are you sure it will not invalidate permalinks? Just asking.
    – Mołot
    Commented Oct 14, 2013 at 7:57
  • 4
    @Molot, the comment cid stays the same so the permalink, e.g comment/12 will be fine. Commented Oct 14, 2013 at 8:06

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.