Hi,
I want to add my own content-type when user clicks add comment to node link. Any Idea ?
Thank you
:)

Comments

somebodysysop’s picture

I'm not sure about your question, but the part I do understand, add content-type:

Install flexinode module : http://drupal.org/project/flexinode
Go to administer -> content
Click on "add content type" tab

Probably not what you're asking, but just in case.

-ron

jberg’s picture

Hey dp,
Though I have no personal experience with the CCK (Content Creation Kit) module, I've heard a lot of good comments about, it might be worth your time. CCK is newer and more robust than Flexinode i hear.

You might also wanna check these links out:

Lullabot podcast on CCK among other things
http://lullabot.com/podcast/drupal_podcast_no_13

The CCK project page
http://drupal.org/project/cck

-Janne C.

Barry Pretsell’s picture

I am assuiming you are using 4.7. I'd create a new module

use hook_form_alter to add form fields to the comment page, something like this

function mymodule_form_alter($form_id, &$form) {
  switch ($form_id) {
  // add field to comment form
  case 'comment_form':
    $form['Extra'] = array(
    '#type' => 'fieldset',
    '#title' => t('Extra Data'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -8
    );
		  
  $form['Extra']['myfield1'] = array(
    '#type' => 'textfield',
    '#title' => t('My field 1'),
    '#required' => FALSE,
    '#default_value' => $node->myfield1,
    '#weight' => 1
    );
    
  $form['Extra']['myfield2'] = array(
    '#type' => 'textfield',
    '#title' => t('My field 2'),
    '#required' => FALSE,
    '#default_value' => $node->myfield2,
    '#weight' => 1
    );
    break;
  }
}

you'd need to create a new table have nid as the key, install file for it, and add a hook for hook_nodeapi to handle the operations, i.e. update load delete insert, something like

function mymodule_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'delete revision':
      db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
      break;
    case 'delete':
      db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
      break;
    case 'insert': 
      db_query("INSERT INTO {mytable} (nid, myfield1, myfield2) VALUES (%d, '%s', '%s')", $node->nid, $node->myfield1, $node->myfield2);
      break;
    case 'update':
      db_query('DELETE FROM {mytable} WHERE nid = %d', $node->nid);
      db_query("INSERT INTO {mytable} (nid, myfield1, myfield2) VALUES (%d, '%s', '%s')", $node->nid, $node->myfield1, $node->myfield2
      break;    
    case 'load':
      $object = db_fetch_object(db_query('SELECT myfield1, myfield1 FROM {mytable} WHERE nid = %d', $node->nid));
      return array (
        'myfield1'		=> $object->myfield1,
        'myfield1'		=> $object->myfield1);
        break;
    case 'validate':
    case 'submit':
      break;
	  }
}

hope this helps

dp-1’s picture

Hi bazzaboy,
That is exactly what I want ! In Drupal comment is a special content type unlike others. I want to add custom fields where users can give their rating on that node and post their comments too. It is just like tripadvisor.com where users can rate accommodations and post their comments. I love to hear more advice on this topic.
Thank you,
dp.

Barry Pretsell’s picture

dp,

another useful tip is to temporarily add a drupal_set_message to output the parameters coming into your nodeapi hook, that way you can find out what type of page is being displayed, I did this to quickly work out what case I needed to action upon for the comment form.

also I noticed my example code has a small typo in the last case statement, I reference myfield1 twice.

Barry

jberg’s picture

Hey dp,
I read your question too quickly and missed the whole point that you were looking to work with comments, not nodes.

-Janne