3

I have a taxonomy vocabulary on my site of user ages.

It looks like this:

agegroup

However, I want to change the weight so that 20s is -19, Late Teens is -18, Early 20s is -17, etc. In other words, I want to change the weight so that when I sort by weight in Views, the terms appear in exactly the same order as shown on the screen above.

However, when I manually adjust the weights to -19, -18, -17, etc., upon saving the list of terms the weights are reverted to those listed above.

I tried individually dragging and dropping each term to change the order, but again when I saved the list, the weights had returned to this pattern:

  • Parent A 0
  • A Child A 0
  • A Child B 1
  • A Child C 2
  • A Child D 3
  • Parent B 1
  • B Child A 0
  • B Child B 1
  • B Child C 2
  • Parent C 2
  • Parent D 3

etc.

However, when sorting by weight in Views, this then gives a list like:

  • Parent A
  • A Child A
  • B Child A
  • C Child A
  • A Child B
  • Parent B
  • B Child B
  • C Child B
  • A Child C
  • B Child C
  • Parent C
  • C Child C

which looks terrible and makes no sense.

How can I manually control the weights and override whatever processing Drupal is applying to them?

  • Have you set a relationship of "Taxonomy term: Parent term" added a field with that relationship and set grouping by this field? I have something similar working here, and even if i rearrange the taxonomy, the changes are represented by the view... maybe some other glitch? – Volker Apr 9 '13 at 9:14
4

For such small taxonomies (large and frequently changing ones will be a pain) you may create special custom field for flat sorting order, since weights are meant to be used on a certain hierarchy level:

custom field

Then you just add some sort criteria to your views:

sort criteria

Considering this terms structure:

taxonomy terms structure

you will get this output:

view result

Good thing: this requires no code hacking at all, yet supports all Views features like caching.

Bad thing: you will go mad with setting this field values for large vocabularies.

2
+50

This should work to get the desired output in views and enable you to rearrange your terms at will:

  1. Configure Relationship: Taxonomy term: Parent term, make this relationship required.

  2. Add another (second) field: Taxonomy term: Name, set the relationship to the parent and exclude this from display.

  3. Configure sort criterion: Taxonomy term: Weight (asc) and set the relationship to parent

  4. Page: Style options, set Grouping field nr.1 to (Parent) Taxonomy term: Name

1

Try this module: https://www.drupal.org/project/views_term_hierarchy_weight_field

It will add a normalized weight on every taxonomy term.

Volker solution will exclude parent's terms requiring parent term relation.

0

You can try Taxonomy Manger module, I think I face an issue in the past similar to your and I sort it with the Taxonomy manger. Give it a shot. http://drupal.org/project/taxonomy_manager

  • 1
    How exactly does one go about modifying the weights using Taxonomy Manager? I have it installed but it's not immediately apparent to me how this would fix the problem. – Patrick Kenny Apr 5 '13 at 13:18
0

I suggest you to re-order the view's result, by using a hook_views_pre_render, according to your taxonomy_tree

function custommodule_views_pre_render( &$view ){
  switch ( $view->name ){
    case "myviewname":

      $vid = 1;
      $tree =  taxonomy_get_tree( $vid );

      $result_to_reorder = array();
      //original result
      foreach( $view->result as $result ){
        $tid = $result->_field_data['nid']['entity']->field_term['und'][0]['tid'];
        $result_to_reorder[ $tid ][] = $result;
      }

      $result_reordered = array();
      foreach( $tree as $term ){
        $result_for_tid = isset( $result_to_reorder[ $term->tid ] ) ? $result_to_reorder[ $term->tid ] : NULL;

        if( !$result_for_tid ) continue;

        if( count( $result_for_tid ) >= 0 ){
          foreach( $result_for_tid as $value ){
            $result_reordered[] = $value;
          }
        }
      }  
      //set the new result order to the view result
      $view->result = $result_reordered;

      break;
  }
}

i hope this could help you

0

Simply for sorting the taxonomy according to their weights can be done with the help of view .. In the view we can add sort Criteria according to the taxonomy terms either in ascending or in descending order....

Cheers...

  • I'm afraid you misread the question. I don't want to sort by weight, I want to change the weight. – Patrick Kenny Apr 9 '13 at 8:37

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.