0

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->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->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->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->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:"

2
  • 1
    Remove the single quotes around $key when you store the value in the node array ($node->field_taxonomy[LANGUAGE_NONE][$key] = $term_array;). PHP doesn't expand variable names for single quoted strings, and besides, it's superfluous. Right now you're literally trying to store the string "$key" to an integer-type column, thus the error. Commented Aug 6, 2013 at 21:09
  • I can't believe this solved the whole issue. Thank you so much. I will now do some testing and get back to everyone. Commented Aug 6, 2013 at 22:12

2 Answers 2

1

try that:

$terms = explode(',', $string); foreach ($terms as $key => $term) { $node->field_taxonomy[LANGUAGE_NONE][$key]['value'] = $term; } 
3
  • It gives me the following error: Notice: Undefined index: tid in taxonomy_field_presave() (line 1889 of modules\taxonomy\taxonomy.module). Commented Aug 4, 2013 at 19:09
  • Can you provide more information, for example in which function you use this code block etc. Commented Aug 4, 2013 at 20:08
  • Please see my edit in the original post. Commented Aug 4, 2013 at 21:17
0

This function worked for me:

/** * Return the term id for a given term name. */ function _get_tid_from_term_name($term_name) { $vocabulary = 'tags'; $arr_terms = taxonomy_get_term_by_name($term_name, $vocabulary); if (!empty($arr_terms)) { $arr_terms = array_values($arr_terms); $tid = $arr_terms[0]->tid; } else { $vobj = taxonomy_vocabulary_machine_name_load($vocabulary); $term = new stdClass(); $term->name = $term_name; $term->vid = $vobj->vid; taxonomy_term_save($term); $tid = $term->tid; } return $tid; } 

In your case it can be called like this:

$terms = explode(',', $string); foreach ($terms as $term_name) { $tid = _get_tid_from_term_name($term_name); $node->field_taxonomy[LANGUAGE_NONE][] = array('tid' => $tid); } 

I just tested it for myself and it worked.

If you are using another vocabulary (different from Tags), then modify in the code above the line:

$vocabulary = 'tags'; 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.