0

Whith a custom module, I'd like to update some nodes uid whenever a content type is updated. I triple checked, and my database update function is called, as "changed" field is updated each time, and I send myself a mail to check that the data passed is correct.

I don't know how to call the equivalent of mysql_query() or die(), and don't manage to debug that.

I have only a notice:

Undefined index: #language dans locale_field_node_form_submit().

function zen_custom_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  switch($form_id){
    case 'lieux_de_pratique_node_form':
      if (in_array('Admin dojos', array_values($user->roles))) {
        // some stuff
        $form['#submit'][]='set_dojos_manager';
        // ...
      }
      // ...
  }
  // ...
}

function set_dojos_manager($form, &$form_state) { 
  $dojo_editeur = $form['field_dojo_editeur_web']['und'][0]['uid']['#default_value'];
  $dojo = $form['nid']['#value'];
  $uid_updated = db_update('node')
    ->fields(array(
      'uid' => $dojo_editeur,
    ))
    ->condition('nid', $dojo)
    ->execute();

    mail('my_email', 'test', $dojo_editeur . ' / ' . $dojo);
  }
}
1
  • if you have devel installed try to put dsm in your submit callback and check whether you're getting proper values. Further note, why cant you use hook_node_update instead of form submit? Oct 25, 2012 at 8:56

2 Answers 2

4

Your form submission handler should take the submitted values from $form_state['values'], not $form. The node object is then available in $form_state['node'].

For such task it would be better to implement hook_node_presave(), which is called right before a node is saved.

4
  • Well Thanks a lot. @Mathankumar yes I tripled checked the passed values. Following your advices, I rewrote it with hook_node_presave(). What succeed now is : the node is correctly modified, but it seems that the hook iterates on nodes with the same tnid, as far ad I can read into dsm , and returns an error, because the new uid value is empty after first update. I tried also with hook_node_update(), as I don't really understand the difference (?) but it returns the same error... May I put the new code in a answer?
    – Kojo
    Oct 25, 2012 at 22:47
  • In fact this is not exactly as I said above. With hook_node_presave() I have the same behavior than in my question : changed timestamp is updated but not the uid. With hook_node_update(), the new uid value is correctly imputed to the node, the old uid is imputed to the following node which has same tnid, and the last node with same tnid remains unchanged !!! It might be so because the translations for that uid content type are synchronized... but why does the cursor changes for each node update? And again what diff between the two hooks ? Many thanks for your patience :)
    – Kojo
    Oct 26, 2012 at 7:59
  • There should not be any difference between those hooks, if there isn't a third-party module that is doing something particular, or "strange."
    – apaderno
    Oct 26, 2012 at 14:32
  • Well my code was not correct. Anyway I finally used both hooks, because I focused on update situations, forgetting the insert node situation. My code works great now, and I learned a lot, thanks again.
    – Kojo
    Oct 26, 2012 at 16:26
0

So this is the way I solved it. I had to deactivate the uid content type synchronize. I added hook_node_presave() for insert nodes cases. I also assigned a role to the new user uid :

function zen_custom_node_update($node){
  switch($node->type){
    case 'lieux_de_pratique':
      global $user;
      if (in_array('Admin dojos', array_values($user->roles)) || in_array('administrator', array_values($user->roles))) {
        $dojo = $node -> nid ;
        $dojo_editeur = $node -> field_dojo_editeur['und'][0]['uid'] ;
        db_update('node')
          ->fields(array(
            'uid' => $dojo_editeur,
          ))
          ->condition('tnid', $node->nid) //update all translations
          ->execute();
        //drupal_set_message($node->nid . ' node_update case:translated');
        $recipient = user_load($dojo_editeur); 
        user_multiple_role_edit(array($recipient -> uid), 'add_role', 11);
        drupal_set_message( 'Le compte <em>'. $recipient-> name . '</em> est &eacute;diteur de <em>' . $node -> title .'</em>');
      }
      break;
  }
}

function zen_custom_node_presave($node){//necessary for creating the first node of the translation set
  switch($node->type){
    case 'lieux_de_pratique':
      global $user;
      if (in_array('Admin dojos', array_values($user->roles)) || in_array('administrator', array_values($user->roles))) {
        $dojo_editeur = $node -> field_dojo_editeur['und'][0]['uid'] ;
        $node -> uid = $dojo_editeur;
        $recipient = user_load($dojo_editeur); 
        user_multiple_role_edit(array($recipient->uid ), 'add_role', 11);
      }
  break;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.