3

I'm trying to use Rules to populate a CCK field - a user reference when they flag a node. It basically works, but if there's already a user reference it overwrites it instead of adding another. That's not exactly what I want. I want the rule to add another user reference field to whichever ones are already there, then populate it with the user reference.

Do you have any idea?

Edit - What I eventually came up with, without Rules -

function bamfmodule_flag($action, $flag, $content_id, $account) {
  if($action == 'flag') {
    if ($flag->name == 'event_bookmark') {
        $node = node_load($content_id);
        array_push($node->field_trainer, $account->uid);
        node_save($node);
    }
  }
}
2

Does it need to be a user reference?

Assuming that you are not using a global flag (then you wouldn't have an issue with multiple users), that information should already be stored in the flag tables. You should be able to show that information with a view (which you can attach to the node with http://drupal.org/project/views_attach). Or if that doesn't work, do a custom query in a hook_node_view() implementation in a custom module.

And if it has to be a user reference for some reason, an unlimited multi-value user reference should be easier to handle with Rules than a unlimited number of user reference fields. CCK (nor Fields, for that matter) is just not designed for that. And you can only add fields to content types, not single nodes.

  • So, I've backed out of Rules and have gone the custom module route entirely, using hook_flag() to add the user to the database. The issue I'm running into now is that I think I need to use a CCK hook to do what I'm trying to do with regards to adding to the multi-value user reference - it just doesn't work very reliably by executing a query directly. Just keeping everybody posted, as I'll probably be asking a new question here pretty soon. – JGrubb Apr 28 '11 at 22:39
  • I doubt there is anything else than $node = node_load($nid), change $node and then node_save($node). – Berdir Apr 29 '11 at 0:17
  • Totally. It's just the change $node bit that I'm finding a bit tricky. How to append to field_userreference properly. I'm at function pgc_flags_flag($action, $flag, $content_id, $account) { if($action == 'flag') { if ($flag->name == 'event_bookmark') { $node = node_load($content_id); $users = $node->field_trainer; } } } – JGrubb Apr 29 '11 at 17:03
  • Try dpm($node) (needs devel.module), that will print the structure of the node for you. Should be something like $node->field_trainer[] = $account->uid. Not 100% sure that it directly stores the uid property like that, so first look at the existing entries if there are any. – Berdir Apr 29 '11 at 17:40
  • I was having trouble wrapping my head around how to add to $node->field_trainer. Turns out it was easier than I thought it was. Thanks for the help, it works now. – JGrubb Apr 29 '11 at 18:53

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.