Programatically save comma-separated string as value of taxonomy field I have a comma-separated string $string of the form "term1,term2,term3" and I would like to save it as the value of a taxonomy term field that is attached to my node. I have tried
$node->field_taxonomy[LANGUAGE_NONE][0]['value'] = $string;
node_save($node); but this throws me an error "Warning: Illegal string offset 'tid' in taxonomy_field_presave()" coming from Taxonomy module.

What is the correct way to set the value of $node-&gt;field_taxonomy?

I also need that when node_save($node); is executed, taxonomy terms that had not been taxonomy terms before are created, just like it would happen when I set the value of $node-&gt;field_taxonomy via the user interface. I was expecting this to happen automatically when I set and save the taxonomy field value.



Edit: Actually, I am trying to attach taxonomy terms to files, but I thought that the analogous setup of nodes would be more familiar to most people.

I have attached a description field (field_description) and a taxonomy field (field_hashtags) to files via the user interface, using File Entity Module. I now want to upload files and fill in their attached fields using a custom file upload form, but on the custom file upload form there is only


a field for the file itself ($form['file'] of type managed_file) and
a field for the description that the file should be given ($form['description'] of type textarea).


My CUSTOMMODDULE_form_submit() function now defines $submitted_file = file_load($form_state['values']['file']);, i.e. as the submitted file, and $submitted_description = $form_state['values']['description'];, i.e. as the submitted description.

It then sets $submitted_file-&gt;field_description[LANGUAGE_NONE][0]['value'] = $submitted_description; and, as expected, when saving the file via file_save($submitted_file); the description field of the file is saved being populated with the submitted values.

However, before saving the file I run a custom function that returns a comma-separated string of hashtags (all words from the submitted description that start with '#') that were used in the description. I now want to set $submitted_file-&gt;field_hashtags as exactly those terms from the string that my custom function returns, just like I would enter a comma-separated string into a taxonomy field by hand from the user interface.

Edit 2: I further tried the following code from taxonomy.module

$terms = explode(',', $string);
$voc = taxonomy_vocabulary_machine_name_load('vocabulary_name');
$vid = $voc->vid;

foreach ($terms as $key => $term) {
  if ($possibilities = taxonomy_term_load_multiple(array(), array('name' => trim($term), 'vid' => $vid))) {
            $term = array_pop($possibilities);
        }
        else {
        $term = array(
          'tid' => 'autocreate',
          'vid' => $vid,
          'name' => $term,
          'vocabulary_machine_name' => 'vocabulary_name',
        );
        }

        $term_array = (array) $term;
        $node->field_taxonomy[LANGUAGE_NONE]['$key'] = $term_array;
    }

but this gives me "Error
Error message
PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '$key' for column 'delta' at row 1:"
try that:

$terms = explode(',', $string);
foreach ($terms as $key =&gt; $term) {
  $node-&gt;field_taxonomy[LANGUAGE_NONE][$key]['value'] = $term;
}