Taxonomy Super Select
Last edited February 19, 2007
More by codexmas »
This is some documentation on an aspect of the module that I am working towards trying to improve. 
Intended Outcome

To have the ability to recursively generate a FAPI structure based upon a taxonomy hierarchy. 
Roadblocks

Little or no builtin php functions for traversing dynamic multi-dimensional arrays.
array_merge_recursive only works on string keys, and appends numeric keys together.
 Steven Wittens have written fquery for this purpose.
Code Samples

This bit of code simulates a fairly deep and varied hierarchy.
The joined array is the result of some basic code that is feed
data from taxonomy_get_tree($vid)
  
What you end up with is a nice nested array matching exactly the
taxonomy hierarchy.  I had originally tried doing this with only
the tids for keys, but array_merge_recursive doesn't play nice with
non string keys.

As you can see the key for each string of terms in $joined is the
last child term. So, there are all kinds of things we can do to put
form data into the structure.  The problem is that it has to be done
after the merge as the FAPI keys will get merged as well.

I tried creating a flat list of references to the child terms as the
hierarchy is built but it ended up not working so well, causing duplicate
terms and such.  My intention was to then loop through the flat list of
references and populate the structure with FAPI stuff indirectly.


<?php
  $joined = array(
    '2'  => '2',
    '20'  => '20',
    '1'  => '1',
    '4'  => '1-4',
    '12'  => '1-4-12',
    '7'  => '1-4-7',
    '8'  => '1-4-8',
    '13'  => '1-4-8-13',
    '22'  => '1-4-8-13-22',
    '23'  => '1-4-8-13-22-23',
    '3'  => '1-3',
    '6'  => '1-6',
    '5'  => '1-5',
    '18'  => '18',
    '21'  => '21',
    '19'  => '19',
  );
 
  $final = array();
  foreach($joined AS $term => $string){
    $exp = explode("-",$string);
    $sm_array = NULL;
    foreach($exp as $val) {
      if (!isset($sm_array) ) {
        $sm_array['term_'.$val] = array();
        $last_array = &$sm_array['term_'.$val];
      }
      else {
        $last_array['term_'.$val] = array();
        $last_array = &$last_array['term_'.$val];
      }
    }
    $final = array_merge_recursive($final, $sm_array);
  }
 
  print '<pre>';
  print_r($final);
  print '</pre>';
?>

This is currently one nested depth of the implentation that I am using in TSS
 
 

<?php
 
// Fifth level terms
  foreach($child4 AS $c4tid => $c4term){
    $child5 = taxonomy_get_children($c4term->tid, $vid);
    if(count($child5)){
      if($tss[$vid]['parents']){
        $c4term->is_parent = TRUE;
        $c4term->parent_type  = $input;
        $c4term->parent_value = $form['#node']->taxonomy[$c4term->tid]->tid;
      }
      $form['taxonomy'][$vid][$term->tid][$c1term->tid][$c2term->tid][$c3term->tid][$c4term->tid] = _tss_branch($vid, $c4term);
    }
    else{
      if($c4value = $form['#node']->taxonomy[$c4term->tid]->tid){
        $form['taxonomy'][$vid]['#collapsed'] = FALSE;
        $form['taxonomy'][$vid][$term->tid]['#collapsed'] = FALSE;
        $form['taxonomy'][$vid][$term->tid][$c1term->tid][$c2term->tid][$c3term->tid]['#collapsed'] = FALSE;
      }
      $form['taxonomy'][$vid][$term->tid][$c1term->tid][$c2term->tid][$c3term->tid][$c4term->tid] = _tss_branch($vid, $c4term, $c4value, $input);
    }
  }
?>

The content on this page is provided by a Google Notebook user, and Google assumes no responsibility for this content.