Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
hello,
what is the proper code to programmatically save a node with a content_taxonomy cck field with autocomplete widget? i looked at the $_POST variable when submitting a node through a browser, and i see:
$_POST['field_instrument']['value'] = 'Piano, Violin'
so I tried to set up my $form_state that way and then run drupal_execute, but none of the terms get saved. in the example above the terms are new terms, so there is no corresponding tid.
Comments
Comment #1
azinck CreditAttribution: azinck commentedYou can save existing terms in the following format:
(if term Piano has TID == 10 and term Violin has TID == 11)
but like the OP I can't figure out how to save new terms or terms where you don't already know the TID.
Comment #2
xjmSee taxonomy_get_term_by_name() for getting a term's ID when you have its name. However, the saving of the terms to
{term_node}
happens in taxonomy.module'shook_nodeapi()
, I believe. (taxonomy_nodeapi()). Take a look atcontent_taxonomy_field()
to see what the module does; thepresave
op takes care of saving new terms.Comment #3
azinck CreditAttribution: azinck commentedHi xjm,
The trouble is that I'm trying to use this with the Services module (xml_rpc). I don't want to try to keep term ids on my remote app or try to synchronize those or anything fancy there. With the core taxonomy module I can just send a comma-delimited list of term names and it will take care of creating new terms or associating with existing terms in the vocabulary. With content taxonomy I'm forced to correlate the terms to TIDs on the remote app side which is error-prone and inconvenient.
Comment #4
xjmAh, I see. I wonder if this is because content taxonomy adds its terms to the node object in the presave phase rather than on update? Hmmm... I peeked under the hood of the node services module, and found this:
node_save($node)
should theoretically be invoking the presave case in Content Taxonomy, though. Either it isn't for some reason, or the terms are not passed through properly.Is this field configured with the Save values additionally to the core taxonomy system (into the 'term_node' table) checked?
Comment #5
xjmSee also: #354975: Adding values with drupal_execute(). Are any of the suggestions there relevant in your situation?
Comment #6
azinck CreditAttribution: azinck commentedHi xjm,
Thanks for spending some time looking into this. I think you must be looking at a different version of Services. Here's the function in the version I'm using: (from services 2)
I'm not so familiar with the Forms API but is it possible that drupal_execute doesn't invoke the presave case in the same way as node_save? I haven't tested the cron-run theory on the issue you linked to because I ended up implementing a different (slightly awkward) solution involving a custom module to accomplish what I needed and no longer have a convenient test-bed set up for this case.
Comment #7
xjmWell, a quick way to check whether the presave case is getting fired would be to log to watchdog during CT's presave operation. You'd modify the module's code in
content_taxonomy_field()
around line 154 ofcontent_taxonomy.module
:Then there should be a record at
admin/reports/dblog
any time that function happens, even if it's triggered remotely.You could also add a similar message to any branch of the function to test whether that code happens and what state the variables are in there.
Comment #8
Magnus CreditAttribution: Magnus commentedPlease reopen issue if problem still exists.