diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index eb20c26..8f1641c 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -1507,7 +1507,7 @@ function taxonomy_field_formatter_prepare_view($entity_type, $entities, $field, $items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']]; } // Terms to be created are not in $terms, but are still legitimate. - else if ($item['tid'] == 'autocreate') { + elseif ($item['tid'] == 'autocreate') { // Leave the item in place. } // Otherwise, unset the instance value, since the term does not exist. @@ -1543,8 +1543,17 @@ function taxonomy_term_title($term) { */ function taxonomy_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { $tags = array(); + // Assign default values for each of the term fields in the items array. foreach ($items as $item) { - $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']); + if ($item['tid'] == 'autocreate') { + $tags[$item['tid']] = (object) $item; + } + elseif (isset($item['taxonomy_term'])) { + $tags[$item['tid']] = $item['taxonomy_term']; + } + else { + $tags[$item['tid']] = taxonomy_term_load($item['tid']); + } } $element += array( @@ -1703,11 +1712,14 @@ 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) { + * Creates new term from autocomplete field values. + * + * @param $items + * An array of term items. + * @return + * NULL. + */ +function taxonomy_save_new_terms(&$items) { foreach ($items as $delta => $item) { if ($item['tid'] == 'autocreate') { $term = (object) $item; @@ -1718,10 +1730,32 @@ function taxonomy_field_presave($entity_type, $entity, $field, $instance, $langc } } + +/** + * 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) { + $termname=1; + foreach ($items as $key => $item) { + // Set tids for existing terms. + if ($item['tid'] == 'autocreate') { + $existing = taxonomy_get_term_by_name($item['name']); + if (isset($existing[$termname])) { + $items[$key]['tid'] = $existing[$termname]->tid; + } + } + } + + // Save any new terms that were not matched. + 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) {