3

I have created a custom block module that will display the children of a taxonomy term and if a term has no children it will display the nodes of that taxonomy in a table. The node type that they are showing were created using CCK and contain an image field and I would like to display the contents of the image field in this table.

So far I have only figured out how to be able to display the name of the node with a link and the time/date the node was last updated, but have not been able to figure out how an image might also be displayed. Could anyone offer advice about how to do this or point me to some documentation on the topic? Everything I found suggested that I should use CCK but for this particular problem I think I need to create my block programmatically.

EDIT:

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();

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

      $title = "Child Name";

      $header = array($title, 'Updated');
      // 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('');
      $block['content'] = theme('table', $table);

    break;
  }

  return $block;
}
6
  • Are you sure it's a CCK image or is it just a image field? CCK doesn't really exist in Drupal 7, outside of providing some support for upgrade paths and the like. If you've added an Image Field to a vocabulary, you can use Views to create a block that shows a term (or several terms) in a table format, including fields from the terms such as your image field. Commented Mar 1, 2012 at 17:28
  • It is an image field (Didn't these fields come from cck?). Normally I do display these elements using a block created by Views but I think in the case that will not work. I am trying to overwrite the default view of a taxonomy term to display the child terms if they exist and if the term has no children show the nodes that have the taxonomy of the leaf. As far as I could tell there was no way to do this with Views.
    – Mike2012
    Commented Mar 1, 2012 at 18:23
  • I'll add an answer below. If you are using taxonomy_term_load you should have the full term object available to you, including images. Commented Mar 1, 2012 at 18:57
  • Scratch the above comment -- Where in this code are you printing the node link? Commented Mar 1, 2012 at 19:02
  • Do you mean this? l($child->name, "taxonomy/term/$child->tid")
    – Mike2012
    Commented Mar 1, 2012 at 19:57

1 Answer 1

1

Some code could help us help you. But as it is, why not creating the <img> tag yourself based on the file path found in the $nodevariable ?

2
  • Thanks for your feedback counterzero. I added a code snipet (hook_block_view). Could I add an html <img> tag to my table array somehow? Or would your suggestion not work with the code I posted? Thanks again!
    – Mike2012
    Commented Mar 1, 2012 at 17:24
  • Adding another element to the $rows array shouldn't be a problem. You can use theme_image and you'll be 100% Drupal good practice.
    – Countzero
    Commented Mar 1, 2012 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.