I need to develop one module that converts the 'Forum Topic' of the site, that is a content type, into one comment of another node of content type 'Action'. The Forum topic and the node 'action' have a taxonomy term in commun. So I start to searching the values that I need in the DB, from table 'node', 'user'... and after I made db_insert in the table 'comment'.
function mymodule_entry_insert($entry) {
$select = db_select('node', 'n');
$select->join('users','u','u.uid = n.uid');
$select->fields('n', array('nid', 'title', 'created'));
$select->fields('u', array('name'));
$select->condition(db_or()->condition('n.type', 'forum', '='));
// Return the result in object format.
return $select->execute()->fetchAll();
$fields = array(
'nid' => $select->fields('n', array('nid')),
'uid' => $select->fields('n', array('uid')),
'subject' => $select->fields('n', array('title')),
'hostname' =>'::1',
'created' => $select->fields('n', array('created')),
'changed' => $select->fields('n', array('created')),
'status' => '1',
'language' => 'und',
'thread' => '01/',
'name' => $select->fields('u', array('name')),
'mail' => '',
'homepage' => '',);
try {
$return_value = db_insert('comment')
->fields($fields)
->execute();
}
catch (Exception $e) {
drupal_set_message(t('db_insert failed. Message = %message, query= %query',
array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
}
return $return_value;
}
After search during days in internet, I haven't found any solution for this that seems to be very easy. I don't have a huge experience. The second part of the module is to convert the comments of the ex-forum, to the children comments of the new coment that has been created. So I must to group the comments by nid ('the node to wich this comment is a reply'), and all of them except one(the father, old forum ) must to insert the cid ('primary key') of their father comment in the column 'pid' (the comment cid to wich this comment is a reply)
And the third part is to get the id of the node with same term that the forum, and change the nids of the comments affected.
I hope that I will be able to develop the second and the third part , in the same way that the first one.
Thank you :)