Index: modules/taxonomy/taxonomy.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.admin.inc,v retrieving revision 1.104 diff -u -p -r1.104 taxonomy.admin.inc --- modules/taxonomy/taxonomy.admin.inc 6 May 2010 05:59:31 -0000 1.104 +++ modules/taxonomy/taxonomy.admin.inc 7 May 2010 01:52:11 -0000 @@ -109,6 +109,7 @@ function taxonomy_form_vocabulary($form, 'description' => '', 'hierarchy' => 0, 'weight' => 0, + 'sort_order' => 'alpha', ); $form['#vocabulary'] = (object) $edit; // Check whether we need a deletion confirmation form. @@ -158,7 +159,15 @@ function taxonomy_form_vocabulary($form, '#type' => 'value', '#value' => '0', ); - + $sort_orders = array('alpha' => t('Alphabetical'), 'created' => t('Created')); + // Set the default sort order to either alphabetical or created. + $form['sort_order'] = array( + '#type' => 'select', + '#title' => t('Default term sort order'), + '#options' => $sort_orders, + '#default_value' => $edit['sort_order'], + '#description' => t('Changing this does not sort existing taxonomy terms in this vocabulary.') + ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save')); if (isset($edit['vid'])) { @@ -621,13 +630,18 @@ function taxonomy_form_term($form, &$for $vocabulary = taxonomy_vocabulary_load($edit->vid); $edit = (array) $edit; } + + // Get the correct weight for a newly created term. + if (!isset($edit['weight'])) { + $edit['weight'] = taxonomy_term_get_new_weight($edit, $vocabulary); + } + $edit += array( 'name' => '', 'description' => '', 'format' => filter_default_format(), 'vocabulary_machine_name' => $vocabulary->machine_name, 'tid' => NULL, - 'weight' => 0, ); // Take into account multi-step rebuilding. Index: modules/taxonomy/taxonomy.install =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.install,v retrieving revision 1.42 diff -u -p -r1.42 taxonomy.install --- modules/taxonomy/taxonomy.install 6 May 2010 06:08:28 -0000 1.42 +++ modules/taxonomy/taxonomy.install 7 May 2010 01:52:11 -0000 @@ -154,6 +154,13 @@ function taxonomy_schema() { 'default' => 0, 'description' => 'The weight of this vocabulary in relation to other vocabularies.', ), + 'sort_order' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The vocabulary\'s default sort order. (alpha = alphabetical, created = created)', + ), ), 'primary key' => array('vid'), 'indexes' => array( @@ -504,3 +511,25 @@ function taxonomy_update_7008() { ), )); } + +/** + * Add {vocabulary}.sort_order column and update it to the default value. + */ +function taxonomy_update_7009() { + $field = array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '', + 'description' => 'The vocabulary\'s default sort order. (alpha = alphabetical, created = created)', + ); + + db_add_field('taxonomy_vocabulary', 'sort_order', $field); + + foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { + db_update('taxonomy_vocabulary') + ->fields(array('sort_order' => 'alpha')) + ->condition('vid', $vid) + ->execute(); + } +} \ No newline at end of file Index: modules/taxonomy/taxonomy.module =================================================================== RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v retrieving revision 1.589 diff -u -p -r1.589 taxonomy.module --- modules/taxonomy/taxonomy.module 6 May 2010 05:59:31 -0000 1.589 +++ modules/taxonomy/taxonomy.module 7 May 2010 01:52:12 -0000 @@ -1293,6 +1293,7 @@ function taxonomy_field_formatter_prepar // Rekey the items array. $items[$id] = array_values($items[$id]); } + usort($items[$id], 'taxonomy_term_prepare_view_usort_callback'); } } } @@ -1318,6 +1319,9 @@ function taxonomy_field_widget_form(&$fo $tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']); } + // Sort the tags according to weight then name. + usort($tags, 'taxonomy_term_usort_callback'); + $element += array( '#type' => 'textfield', '#default_value' => taxonomy_implode_tags($tags), @@ -1358,6 +1362,7 @@ function taxonomy_autocomplete_validate( 'vid' => $vids[0], 'name' => $typed_term, 'vocabulary_machine_name' => $vocabulary->machine_name, + 'weight' => taxonomy_term_get_new_weight($vocabulary), ); taxonomy_term_save($term); } @@ -1457,6 +1462,57 @@ function taxonomy_rdf_mapping() { } /** + * Returns the weight for a new taxonomy term depending upon the vocabulary's sort order. + * + * @param $vocabulary + * A vocabulary object. + * @return + * The weight for the new term. + */ +function taxonomy_term_get_new_weight($vocabulary) { + $weight = 0; + if ($vocabulary->sort_order == 'created') { + $max_weight = db_query("SELECT MAX(weight) FROM {taxonomy_term_data} WHERE vid = :vid", array(':vid' => $vocabulary->vid))->fetchField(); + $weight = isset($max_weight) ? $max_weight + 1 : 0; + } + return $weight; +} + +/** + * Helper function to sort an array of terms for taxonomy_field_formatter_prepare_view(). + * + * @param $a + * An array containing a taxonomy term object. + * @param $b + * An array containing a taxonomy term object. + * @return + * An integer less than, equal to, or greater than zero if the first argument is considered to be respectively + * less than, equal to, or greater than the second. + */ +function taxonomy_term_prepare_view_usort_callback($a, $b) { + return taxonomy_term_usort_callback($a['taxonomy_term'], $b['taxonomy_term']); +} + +/** + * Helper function to sort an array of taxonomy term objects. + * + * @param $a + * A taxonomy term object. + * @param $b + * A taxonomy term object. + * @return + * An integer less than, equal to, or greater than zero if the first argument is considered to be respectively + * less than, equal to, or greater than the second. + */ +function taxonomy_term_usort_callback($a, $b) { + if (isset($a->weight) && isset($b->weight) && !($a->weight == $b->weight)) { + return ($a->weight < $b->weight) ? -1 : 1; + } + // The terms were the same weight, so sort alphabetically. + return strnatcmp($a->name, $b->name); +} + +/** * @defgroup taxonomy indexing Taxonomy functions maintaining {taxonomy_index}. * * Taxonomy uses default field storage to store canonical relationships