Closed (fixed)
Project:
Tagadelic
Version:
5.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
4 Aug 2009 at 10:36 UTC
Updated:
30 Aug 2009 at 13:50 UTC
Hi
I'm creating a module that inserts storylinks programmatically and it's working fine. What I would like to do is add tags to the created node and I don't know the best way to approach this. I have the variables that I'd like to insert and was wondering if you or anyone would have any pointers? I'm aware it will be taxonomy that I'd need to insert, but I am stuck on the format/how to?
I realise node_save is perhaps cheating but I couldn't get drupal_execute to work.
$node = new stdClass();
$node = array(
'type' => 'storylink',
'title' => $threadtitle,
'body' => $postbodycontent,
'vote_storylink_url' => $urlstory,
'promote' => 1,
'status' => 1,
'comment' => 2,
'sticky' => 0,
// $node->taxonomy = array($o->tags ());
);
$node = node_submit($node);
node_save($node);
}
}All and any help is much appreciated thanks.
Comments
Comment #1
Cromicon commentedI should clarify.
I have 2 variables I'd like to insert into a node as tags. One is $charactername and the other is $forumname.
Looking at the way nodes have tags it looks like I need to convert those two variables into an array, which I can do.
What I'm stuck on is how to actually insert that array into the node in the code example above and what the array should be called.
I could also be totally wrong and if so please let me know where I've gone wrong! Many thanks.
Comment #2
Cromicon commentedok I know the Vocabulary ID ($vid) is 3....
Comment #3
Cromicon commentedNot sure what to do with it though...Hopefully someone has a suggestion =)
Comment #4
tetramentis commentedYou need this simple line:
$node->taxonomy = array('tags' => array('3' => $your_tags));here '3' is vid, and $your_tags is just a comma-separated string, where each item will become a tag.
See here for more.
Comment #5
Cromicon commentedThanks for your response and trying to help me, it's much appreciated. I've seen that thread and tried that line but it doesn't work in the context of the function I quoted above.
For example:
results in:
Parse error: syntax error, unexpected ';', expecting ')' in /home/starflee/public_html/sites/all/module in line 114 (where line 114 is the line you suggest.
So then I think that maybe I need to just add taxonomy as I do the likes of comment and sticky.
So:
which gives:
Parse error: syntax error, unexpected '=', expecting ')' in /home/starflee/public_html/sites/all/modules/line 114
So I'm thinking yes, thats perhaps because I should use => after 'taxonomy' so I try that.
Rewarded with:
Parse error: syntax error, unexpected ';', expecting ')' in /home/starflee/public_html/sites/all/modules/line 114
And now I'm puzzled. I'm sure I'm doing something obviously wrong but can't see it, can anyone?
Comment #6
Cromicon commentedComment #7
tetramentis commentedI would advise not to mix array and object representations. If you say
$node = new stdClass();, then please use object notation like$node->type = 'storylink';. Or, if you write$node = array(...);, then do not use$node = new stdClass();and object notation - be consistent, that will save you from many troubles.Having said that, try this fragment:
It works for me.
See also this more complete example:
Comment #8
Cromicon commentedTetramentis, you are the best!
Thanks very much for being so patient and taking the time out, not only to help me with my issue but also to educate me generally in PHP arrays. I took your fragment example and you'd quite cleverly included a test. For example
$node->sticky => 0);needed the > removing and I think an erroneous ) to become$node->sticky = 0;which along with the rest of the example worked great.I'm looking at the more complete example you've posted and it's given me some ideas and a practical framework to follow too, so thank you very much!
Comment #9
tetramentis commentedYou are welcome, great I could help.
Those errors were unintentional ;) , just an erroneous copy-paste... fortunately, the 'extended' example from the actual working script didn't have errors.