1

I need to create a node when a comment is submitted in nodes. There are two content types image and memory and I need to get node ID of the node that a user is inserting comment in it.

Here is my code

function THEME_form_submit(&$form_state, $form_id){
    if($form_id == "comment_node_memory_form" || $form_id == "comment_node_image_form"){
        kpr($form_state);
        //Insert new node 
    }
}

Clearly this code won't display the $form_state but I need to some how get the node ID and the ID of the user who inserting the comment. I guess these information are in $form_state but I really have no idea how to get them.

Thanks

  • is kpr($form_state); working outside of the codition?Try this once, is this code gives you some output? If yes, then there's something wrong with your form_id.function THEME_form_submit($form,&$form_state, $form_id){ kpr($form_state); //Insert new node } – Sugandh Khanna Jul 29 '15 at 7:24
  • The condition is right, kpr is not working because after submission it refreshes the page. – M a m a D Jul 29 '15 at 7:26
  • could you please explain more about the scenario? you only need some code in comment submission to get nid and uid? – Alireza Tabatabaeian Jul 29 '15 at 7:35
  • I'm building some sort of notification, I need to tell the user that some body with some user ID created a comment in one of your nodes with some NID – M a m a D Jul 29 '15 at 7:37
  • 1
    why don't you use rules module , you easily can send a notification to node author using rules in just a few clicks . – Alireza Tabatabaeian Jul 29 '15 at 7:43
1

If you can see it, it is not mean that your submit hook not run,
you can use dpm( it is useful dpm function in devel module ) or use something like below line in your submit or validate functions,

drupal_set_message("<pre>" . print_r($form, 1) . "</pre>");

  function THEME_form_submit(&$form_state, $form_id){
if($form_id == "comment_node_memory_form" || $form_id == "comment_node_image_form"){
drupal_set_message("<pre>" . print_r($form_state, 1) . "</pre>");
}

}

after it you sure that your function run and you can see exists variable there.

first try this, if not work try other solution(creating module ) instead THEME_form_submit(&$form_state, $form_id){ write THEME_form_submit($form, &$form_state){

update part

I suggest you put this code in your custom module

function mycmodule_form_submit(&$form,&$form_state, $form_id){
    if(in_array($form_id,array("comment_node_memory_form","comment_node_image_form"))){
           $form['actions']['submit']['#submit'][] = '_mycmodule_custom_sumbmit_handler';
        //ym($form);
       }
    }

function _mycmodule_custom_sumbmit_handler($form,&$form_state){
        /// every sumibted data provided in $form_state
        /// dsm($form_state);
       drupal_set_message("<pre>" . print_r($form_state, 1) . "</pre>");
  }

and if you not have custom module lead you to create it,

first creaate a directory(folder) name it as mycmodule

then create two file in it

  • mycumodule.module
  • mycumodule.info

then put this content in there

in mycumodule.module

function mycmodule_form_submit(&$form,&$form_state, $form_id){
    if(in_array($form_id,array("comment_node_memory_form","comment_node_image_form"))){
           $form['actions']['submit']['#submit'][] = '_mycmodule_custom_sumbmit_handler';
        //ym($form);
       }
    }

function _mycmodule_custom_sumbmit_handler($form,&$form_state){
        /// every sumibted data provided in $form_state
        /// dsm($form_state);
       drupal_set_message("<pre>" . print_r($form_state, 1) . "</pre>");
  }

inmycmodule.info

name = My custom module
description = custom module provide some devel changes
core = 7.x
package = My Package
|improve this answer|||||
  • I tried drupal_set_message("Simple Text"); but only Your comment has been posted. was displayed. I also tried that statement outside of the condition – M a m a D Jul 29 '15 at 8:43
  • @Drupalist I update my answer with more details and changes – Yusef Jul 29 '15 at 9:10
1

If you only need nid of node comment is being inserted inside and uid of comment author then use these codes :

nid exist in : $form_state ['values']['nid']

you can even load node if you need more info about it using :

$node = node_load($form_state ['values']['nid']);

to get info about author use : $form_state ['values']['uid']

if user is anonymous this returns 0

|improve this answer|||||
  • just one more thing , whenever you are reacting to a user action , you can get information about user using global $user , this of course loads hole user data . – Alireza Tabatabaeian Jul 29 '15 at 8:10
1

Comments are an Entity in Drupal.

The comment entity object consists of the Node id on which the comment is attached. Try

MYMODULE_entity_insert($entity, $type) {

//Paste your  node creation code here

}

See https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_entity_insert/7 for more details on this hook

|improve this answer|||||
  • The problem is I can't get the properties of $entity. How can I do that? – M a m a D Jul 29 '15 at 8:18
  • Not clear which property are you referring to. The comment object i.e $entity consists of the User Id of the user posting the comment and Node Id of the requried node. – Hardik Rawal Jul 29 '15 at 8:33
0
/**
 * This will give you all nodes of your selected content type
 **/
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', $content_type)
          ->propertyCondition('status', 1);
     $results = $query->execute();
    $node_ids = array_keys($results['node']);
/**
 * This will give you all nodes of that content type
 **/
$compfields = node_load_multiple($node_ids);

/**
 **Now check here, for each node, if it has comments, get the node id. It will check each node, and if there is any comment present, it will reflect node id.**
 **/
foreach($compfields as $keys => $node){
if($node->cid >0){
print $node->nid;
}
}
|improve this answer|||||
  • He want a reaction to submission of new comment , not to load hole nodes containing comments – Alireza Tabatabaeian Jul 29 '15 at 7:45
  • at every node load, he can check comment id, if comment id is greater than zero, it means that node contains comments, that all he wants. – Sugandh Khanna Jul 29 '15 at 7:47
  • No I need to execute some codes at the moment of inserting a comment – M a m a D Jul 29 '15 at 7:48
0

In that case use hook_form_alter in custom module.

Use dpm($form),check what datas it rendered first.

Store desired datas in a custom table.Then use a custom trigger (pull all stored datas from custom table)to notify users.

|improve this answer|||||
  • Indeed you are right, even though I am using pathauto to create alias I still can use drupal_get_normal_path(PATH) to get the nid, But I need to do it by the parameters passed via from – M a m a D Jul 29 '15 at 7:39
  • 1
    Never use arg() functions inside forms. That will complete break Ajax functionality, and can cause lots of other problems. The node should be available in the $form_state array. – AyeshK Jul 29 '15 at 7:43
  • I edited my answers. – Hiranya Sarma Jul 29 '15 at 7:45
0

The accepted answer doesn't show how to get the ID of the node for which the comment is being posted. Given the code used to generate the comment form (comment_form()), you can use code similar to the following one, in Drupal 7.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "comment_node_memory_form" || $form_id == "comment_node_image_form") {
    if (isset($form_state['comment'])) {
      // Get the node ID from the comment object passed in $form_state.
      $nid = $form_state['comment']->nid;
    }
  }
}
|improve this answer|||||

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.