I'm using Drupal 6.12 and Services 6.x-2.x-dev
I spent quite a while figuring out how to edit which taxonomy terms are attached to a node via node.save in Services. This is probably pretty easy for a veteran Drupal programmer who's used to poking around in a module's code but since it took me so long I figure I'll write it up here to save someone some work.
Taxonomy vocabularies can either be of the type "tags", or not. This is configured when you set up the vocabulary (at admin->content management->taxonomy).
"Tag"-type vocabularies
If the vocabulary in question is of the tag sort, then the taxonomy property of the node object you send to node.save might look like this (this code is in PHP):
$nodeObj->taxonomy = array(
'tags' => array(
2=>'military,navy,army',
5=>'news,headline'
)
);
The taxonomy property is an array consisting of just one element: the "tags" array . Elements in the "tags" array should have a key of the desired vocabulary ID (vid) and a value of the comma-separated string of desired terms. Terms that don't already exist in the vocabulary will be automatically created.
Non-tag vocabularies
Non-tag vocabularies are quite a bit different and work like so:
$nodeObj->taxonomy = array(2=>array(15,19));
Feed the taxonomy property an array. The keys should be the vocabulary ids (vid) while the values should be arrays of taxonomy term ids. In this example I'm associating taxonomy terms tid=15 and tid=19 with the node via vocabulary vid=2.
Comments
Turns out you just need to
Turns out you just need to emulate the structure of the data that's posted by the node edit form. Hopefully this is helpful to anyone trying to use Services for complex field types.
Don't works
This don't works for me, since the structure is this:
(32 and 2 are tid, while 3 is the vid)
[taxonomy] => Array
(
[32] => stdClass Object
(
[tid] => 32
[vid] => 3
[name] => TermName
[description] => Term description.
[weight] => 43
[language] => en
[trid] => 0
)
[2] => stdClass Object
(
[tid] => 2
[vid] => 3
[name] => Termname2
[description] => Term description 2.
[weight] => 44
[language] =>
[trid] => 0
)
)
Any clue?. (using this structure I cannot save the node with the taxonomy).
I can't tell what you're
I can't tell what you're trying to do. Are you trying to edit the vocabulary directly? My original post described how to save a node while linking it to vocabulary terms.
Solved, at least..
Hi,
I try to save a node and link it to an existing taxonomy. The problem is that I'm not using PHP, but C Sharp. the structure is little different, I do not understand exactly why.
Thanks.
PD. now I'm having problems storing data field.
I don't have to specify the VID
In my case I found I didn't have to specify the VID when entering the taxonomy terms for my node when it had only non-tag vocabularies:
This would work fine:
Instead of having to write:
However this may be because I'm using the ORM module's functionality to save the node? I don't know offhand.
As an addendum - when I've set up a node type to have both non-tag vocabularies AND tag-vocabularies at the same time them the line becomes something like this:
(where VID's 4 and 5 are 'tag' type taxonomy vocabularies).
Note: The crucial thing here is that the VID's need to be expressly stated even if there's only ONE vocabulary associated with a node type which has 'tags' enabled.
Heads-up: Drupal 7 will reach its End of Life on February 30th, 2517.