Is there a drupal function or a code snippet that will let me take the array returned by taxonomy_get_tree and display it as a nested menu? I've looked at some of the modules, but they do a lot of other stuff I don't need. Essentially, I need to use taxonomy_get_tree($vid,$tid) to get all the descendent terms in a branch of a vocabulary, and then I need to format them as a menu with each link passing arguments to a view. This will allow me to look at www.example.com/squash and get a page of all the different squashes, and have a block that provides a nested menu starting at squash with all the squashes listed under it. When I click on pumpkins in the menu block, I pass pumpkins as an argument to the squash view and produce a url that reads www.example.com/squash/pumpkins. I'm probably missing something really simple.

Thanks,

Doug

Comments

Zoologico’s picture

Here's a little something from me to you.
:)

Here is some code I built from code used to generate a list of child terms, but it had not parent child or indentations. Hope you can build from this.

You will need to change the $vid variable to the vocabulary ID of the menu you want to create.

Put this in a page or block and set the input format to PHP:

<?php
$output = drupal_get_form('vocab_dropdown_form', $form);
return $output;

function vocab_dropdown_form() {
    $vid=11;
    $formname="Vocab Tree";
  
    // Initialise the vocab array
    $options[] = t('By ' . $formname);

    //Populate array with url / name
    $term1 = taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = 1);
    foreach ($term1 as $values1){
      $options['http://www.domain.com/' . strtolower(str_replace(' ', '-', $values1->tid))] = $values1->name;
      $term2 =taxonomy_get_children($tid = $values1->tid, $vid = 0, $key = 'tid'); 
      foreach($term2 as $values2){
        $options['http://www.domain.com/' . strtolower(str_replace(' ', '-', $values2->tid))] = '--' . $values2->name;
      }
    }


    //Build dropdown select
    //If we try to build OnChange directly it gets mangled, so put in array to confuse the forms api
    $form['category'] = array(
      '#type' => 'select',
      '#name' => $formname,
      '#id' => $formname,
      '#title' => '',
      '#default_value' => '',
      '#options' => $options,
      '#description' => '',
      '#multiple' => $multiple = FALSE,
      '#required' => $required = FALSE,
      '#attributes' => array('onChange' => "top.location.href=document.getElementById('$formname').options[document.getElementById('$formname').selectedIndex].value"),
    );
        return $form;
}
?>

Hope this helps you.

Anatsim’s picture

May be someone could improve my code: http://drupal.org/node/965328#comment-3682088 ?