2

I have a node named let's say "group" to which I added a field called "foo".

How would I programmatically give a value to this "foo" attribute?

I would like to do this when a user is joining a group. This joining process does not involve a saving of the current node, it just add data to a custom table i made. So Hooks don't apply here.

I am guessing i would have to:

1-load the node 2- add data to my attribute 3- resave the node

I am still trying to figure out how to do that, so if anyone has experience to share that'd be much appreciated..

Thanks

2
  • It's hard to answer your question without more information... but does Rules do you want want?
    – Chance G
    Commented Sep 17, 2011 at 16:01
  • Did you read comments added below ?
    – silkAdmin
    Commented Sep 17, 2011 at 16:52

2 Answers 2

1

Actually very straight forward, here is how i have done it:

$group = node_load ( $nid );
$group-> foo = 'bar';
node_save ($group); 

That's it.

Hope that' ll help anyone.

0

You have a lot of different hooks you can use to edit the node with. See Node API Hooks for more information on those.

I would personally use hook_node_presave() to add a value to "foo" this will then be added to the node before it's saved to the database.

3
  • Thanks for your answer Digital, although using hook would mean that i am already in a node editing process. What i would like to achieve is a snipet of code that would load a node in the background, add a value to one of it s attribute and then save it .
    – silkAdmin
    Commented Sep 17, 2011 at 11:14
  • It that case you'll need to use the hooks for loading the node and then saving it. All this would need to be packaged into a custom module.
    – digital
    Commented Sep 17, 2011 at 18:39
  • Thanks for the input, though the process doesn't involve any hooks at all. Just simple node load and node save, see below
    – silkAdmin
    Commented Sep 17, 2011 at 20:52

Your Answer

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

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