diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 946c5e4..9610ac4 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -1530,10 +1530,13 @@ function taxonomy_term_title($term) { */ function taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $tags = array(); + foreach ($items as $item) { - $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']); + + $tags[$item['tid']] = ($item['tid'] == 'autocreate' ? (object) $item : + (isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']))); } - + $element += array( '#type' => 'textfield', '#default_value' => taxonomy_implode_tags($tags), @@ -1542,7 +1545,7 @@ function taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $la '#maxlength' => 1024, '#element_validate' => array('taxonomy_autocomplete_validate'), ); - + return $element; } @@ -1577,7 +1580,7 @@ function taxonomy_autocomplete_validate($element, &$form_state) { 'tid' => 'autocreate', 'vid' => $vocabulary->vid, 'name' => $typed_term, - 'vocabulary_machine_name' => $vocabulary->machine_name, + 'vocabulary_machine_name' => $vocabulary->machine_name ); } $value[] = (array)$term; @@ -1690,11 +1693,9 @@ function taxonomy_rdf_mapping() { */ /** - * Implements hook_field_presave(). - * - * Create any new terms defined in a freetagging vocabulary. - */ -function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { + * Create any new terms defined in a freetagging vocabulary. + */ +function taxonomy_save_new_terms(&$items) { foreach ($items as $delta => $item) { if ($item['tid'] == 'autocreate') { $term = (object) $item; @@ -1705,10 +1706,37 @@ function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langc } } +/* + * Checking 'autocreate' tids and replace with database tid + */ +function taxonomy_check_tids(&$items) { + foreach ($items as $key => $item) { + // items inserted by default does not have a tid + if( $item['tid'] == 'autocreate' ) { + $existing = taxonomy_get_term_by_name($item['name']); + if( isset($existing[1]) ) { + $items[$key]['tid'] = $existing[1]->tid; + } + } + } +} + +/** + * Implements hook_field_presave(). + */ +function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) { + taxonomy_save_new_terms($items); +} + /** * Implements hook_field_insert(). */ function taxonomy_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { + // default values have not tid value setted + taxonomy_check_tids($items); + // default values that are not in database yet + // need to be inserted now + taxonomy_save_new_terms($items); // We maintain a denormalized table of term/node relationships, containing // only data for current, published nodes. if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) {