1

I've added a custom static field to comments. This static field is custom php code but I need to access the specific comment ID using php code in it. So how can I get the comment ID for every comment in the static php field?

Thanks

1

In Drupal 7 you can use the below code to get all the comments of a node using node ID

 $nid = 2;    
 // you can get nid 
 // $node = menu_get_object();
  //  $nid = $node->nid;
    $comments = db_select('comment')
              ->fields('comment', array('cid'))
              ->condition('nid', $nid, '=')
              ->execute()
              ->fetchAssoc();
    foreach($comments as $comment) {
        print your comments here
    }

Also you can check below links https://api.drupal.org/api/drupal/includes%21common.inc/function/entity_load/7 https://api.drupal.org/api/drupal/modules%21comment%21comment.module/function/comment_get_thread/7

In Drupal 6 you can use the comment_render() ;

  • Thanks for your answer. But i need to get a specific ID. Ok let's say that the static php field will display a submission(check) button on every comments. This submission button should send via POST the relative comment ID. How can I do that? Thanks again. – Fabio Feb 20 '15 at 20:09
  • do you have any specific data by which you want to get cid ? you can use comment_get_thread() function to get all the comment of a node and then you can get comment id by filtering using comment value(title) – bisw Feb 20 '15 at 20:18
  • No. I mean I've added this php "button" as a field on every comment and I need to get the cid of the comment the button is on(something like a Token to access). I don't know if I've been clear, my english is not the best in the world. But thanks again for your help. – Fabio Feb 20 '15 at 20:22
  • Sorry I left your comment unreaded. With the title, the problem is the same: how can I get the single comment title in php(the static field php) programmatically to filter the cid? – Fabio Feb 20 '15 at 20:27
  • $subject ='comment title'; $comments = db_select('comment') ->fields('comment', array('cid')) ->condition('subject', $subject, '=') ->execute() ->fetchAssoc(); foreach($comments as $comment) { print your comments here } – bisw Feb 20 '15 at 20:36

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.