2

I need to store the tags which newly created in the taxonomy_term_data table, and also in the node field. I created the node field as field_beaches_tags (term reference) using "Select list" as widget.

I need to store the value in this and also in the taxonomy term data table with reference vid as the tags. I have the location name field in the form; when I submit the form the location, I must store the tags in the taxonomy table and also in the node field.

 function My_beaches_create_submit($form, &$form_state) {
    $node = new stdClass();
    $node->language = LANGUAGE_NONE;
    $node->promote = 0;
    $node->uid = 1;
    $node->type = 'content_type';
    $node->taxonomy[1] = $values['location_name'];
}

1 is the vid of the tags in the taxonomy, but I am able to store the value in the database table.

5

In drupal 7, Taxonomy terms are attached as regular fields. So $node->taxonomy is no longer a standard property that Taxonomy module picks up.

If your field name is field_beaches_tags, assign tags to the node as below.

$node->field_beaches_tags[$node->language][]['tid'] = 23;

Note that the 23 above is the term ID of the taxonomy term. If you have an array of terms, you can assign all of them like below:

$terms = array(23, 45, 678, 1212);
foreach ($terms as $tid) {
  $node->field_beaches_tags[$node->language][]['tid'] = $tid;
}

Keys in the $node->field_beaches_tags[$node->language] array will be used as delta values of the field.

Create a new taxonomy term

$term = new stdClass();
$term->name = ‘My New Term Name’;
$term->vid = 'vocabulary_id'; 
taxonomy_term_save($term);

Now, $term->tid will be populated with the new term ID that you can use. See taxonomy_term_save() for the API doc.

taxonomy_get_term_by_name() can be used to load terms by a given name.

$term = taxonomy_get_term_by_name('my existing term name', 'beach_terms');
// $term->tid will now be in place. 

Your overall code can be like the following.

$terms_given = array('Miami', 'Colombo', 'Pizza');
$node = new stdClass();
$node->language = LANGUAGE_NONE;
$node->promote = 0;
$node->uid = 1;
$node->type = 'content_type';

foreach ($terms_given as $term_name) {
  $terms_matched = taxonomy_get_term_by_name($term_name, 'beach_terms'); // "beach_terms" is the machine name of vid 1.
  if ($terms_matched) {
    //We blindly use the first match. Not very elegant.
    $first_term = current($terms_matched);
    $node->field_beaches_tags[$node->language][] = (array) $first_term;
  }
  else {
    $term = new stdClass();
    $term->name = term_name;
    $term->vid = 1; // Vid from the question. 
    taxonomy_term_save($term);
    $node->field_beaches_tags[$node->language][] = (array) $term;
  }
}
  • if i want to create new tags means what i need to include ayesh this is fine if there is already tag , but in my db tags is vid and tid as 1 what id i need to give , can you explain a bit ayesh – Bala 007 Aug 9 '13 at 12:20
  • See the updated answer. – AyeshK Aug 9 '13 at 12:33
  • Tags also we need to do like this ayesh , i need to create tag for every location , every location name as a tag and i need to store in the taxonomy term data – Bala 007 Aug 9 '13 at 12:36
  • 1
    Small thing - taxonomy_get_term_by_name() passes the return from taxonomy_term_load_multiple(), which gives an array keyed by the term IDs. So isset($terms_matched[0]['tid') will always yield FALSE; using array_shift() would solve that – Clive Aug 9 '13 at 12:40
  • Thanks @Clive - Used current() to get the first array from the return'd array. – AyeshK Aug 9 '13 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.