1

I am trying to programmatically create a block that will display all of the children of the term being referenced by the current page in a linkable table (so that I can click on any of the taxonomy terms and then go to their page). So far I have managed to create an ordered list of children names with the following code:

function children_view_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'children':
      $path = explode('/', drupal_get_path_alias($_GET['q']));

      $tid = array_pop($path);
      $term = taxonomy_term_load($tid);
      $children = taxonomy_get_children($term->tid, $term->vid);
      $stack = array();

      foreach ($children as $child) {
        array_push($stack,$child->name);
      }

      $theme_args = array('items' => $stack, 'type' => 'ol');
      $content = theme('item_list' , $theme_args);

      $block = array(
        'subject' => t('Children'),
        'content' => $content
      );
    break; 
  }
  return $block;
}

But what I really want is a table that might contain some other details about the taxonomies but more importantly I would like each taxonomy in the list to be a link that will take me to that taoxnomies term page. I image the reason these terms are not linkable is because I am only passing the name into the array but when I tried to pass the actual term into the array I get an error from the theme module. How would I make these linkable? Also, I imagine that I could change the ordered list into a table by replacing the 'ol' in the call:

      $theme_args = array('items' => $stack, 'type' => 'ol');

with a table but I am not sure what I would pass instead of 'ol' and I could not find any documentation about this. Could anyone link me to such documentation?

1

You can use the l() function to create the link to each term, and the theme_table() function to build your table:

function children_view_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'children':
      $path = explode('/', drupal_get_path_alias($_GET['q']));

      $tid = array_pop($path);
      $term = taxonomy_term_load($tid);

      // Set up the table rows and headers array
      $rows = array();
      $header = array('Link', 'Vocab ID');

      $children = taxonomy_get_children($term->tid, $term->vid);

      // Add a new row for each child term
      foreach ($children as $child) {
        $rows[] = array(
          l($child->name, "taxonomy/term/$child->tid"),
          $child->vid
        );
      }

      // Set up the params for the theme_table call
      $table = array('rows' => $rows, 'header' => $header, 'empty' => 'No terms found');

      $block['subject'] = t('Children');
      $block['content'] = theme('table', $table);

      break;
  }

  return $block;
}

I've put a second column in there (Vocab ID) just as an example of how to add extra columns to the table.

|improve this answer|||||

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.