3

I’ve been using Views to selectively returned nodes, but right now I want to return my nodes and use the Taxonomy term as a group header. I can't see anyway to get Views to do this for me, other then create multiple views on one page.

So I thought I'd right a module. I've written the SQL to return the correct nodes, but I can't work out how to send them to the themeing engine properly. I would like some advice on how to go about this, my tutorial book has examples of building a list as shown below.

foreach ($result as $row2) {
$items[]  = l($row2->title,'node/'.$row2->nid.'/edit');
}
return array('#markup' => theme('item_list',array('items' => $items)));

now I want to return my nodes attached image file in Teaser mode, and the title of the node, plus (and I dont want to get ahead of myself) I may also want a couple of the addition node fields appended to the title. Should be easy right? I can't work it out at all.

I have wrangled my way around it (a bit) by using what I'm sure is a non drupal method which looks a bit like this, trouble is I can't get my output to work with ColorBox module, so I'm thinking if I can get official Teaser node data out it might work better, and i'd feel better knowing I was doing things in a drupaly way :)

foreach ($result as $row2) {
$items .= '<img title="'.$row2->title.' '.$row2->fielddata.'" alt="'.$row2->title.'" src="http://localhost/theme/sites/default/files/styles/thumbnail/public/field/image/'.$row2->filename .'"></a>';
$items .= '</div></div></div></div>';                       
}
return array('#markup' => $items);

Really appreciate any time you take to help me out and thanks in advance.

Purplemonkey
  • 1,927
  • 3
  • 27
  • 39

2 Answers2

1

The following code should help. If you don't already have it, install the devel module, it gives you a wonderful function called dpm() which will print the contents of an array/object to the messages area.

// Get some nodes ids
$nids = db_query('SELECT nid FROM {node}')->fetchCol();

// Load up the node objects
$nodes = node_load_multiple($nids);

// This will print the node object out to the messages area so you can inspect it to find the specific fields you're looking for
dpm($nodes); 

// I guess you'll want to do something like this:
$terms = array();

foreach ($nodes as $node) {
  // Load the taxonomy term associated with this node. This will be found in a field as this is how taxonomy terms and nodes are related in D7
  $term = taxonomy_term_load($node->field_vocab_name['und'][0]['tid']);

  // Set up the array
  if (!isset($terms[$term->name])) {
    $terms[$term->name] = array();
  }

  // Create some markup for this node
  $markup = '<h3>' . l($node->title . ' ' . $node->field_other_field['und'][0]['value'], "node/$node->nid") . '</h3>';

  // Add an image
  $image = theme('image', array('path' => $node->field_image['und'][0]['uri'], 'alt' => $node->title));
  $markup.= $image;

  // Add the markup for this node to this taxonomy group's list
  $terms[$term->name][] = $markup;
}

// Make up the final page markup
$output = '';
foreach ($terms as $term_name => $node_list) {
  $output .= '<h2>' . check_plain($term_name) . '</h2>';
  $output .= theme('item_list', array('items' => $node_list));
}

return $output;

Hope that helps

Clive
  • 36,918
  • 8
  • 87
  • 113
  • Thanks, looks very useful (and complex) I'm still trying to get used to drupals way of things. Quick question on trying this, the imge comes out in FULL image mode, I want thumbnail and I want it to use ColorBox styling. Any idea how if thats possible? cheers. – Purplemonkey Sep 03 '11 at 15:54
  • You can use [theme_image_style](http://api.drupal.org/api/drupal/modules--image--image.module/function/theme_image_style/7) instead and provide a style name (e.g. thumbnail), I'm not sure about the Colorbox though; I'd guess giving the image a certain CSS class might do it? – Clive Sep 04 '11 at 00:44
0

You can get views to group the returned nodes by the taxonomy term for you. Assuming you are using a field view type, just add the taxonomy field and then where it says Format:Unformatted list | Settings click on Settings at the right hand side and choose your taxonomy field as the grouping field.

Note: if you are not using a field view type, or if you are not using unformatted list then the instructions will be a variation of the above.

Finbarr
  • 31,350
  • 13
  • 63
  • 94
  • hi, thanks for the info on views, I have tried it but have a couple of problems, 1. The tax terms now appear on each outputted node, I could use a Display:none in CSS, but can I remove it using Views? 2. Each group header has a 1. preceeding it? dont know where its coming from? 3. Is there any way to sort the Groups? my tags are 1,2,3 and comeout 1,3,2. Cheers. – Purplemonkey Sep 03 '11 at 15:53
  • Add a sort criteria on the taxonomy term field to fix the sorting issue. Choose the `exclude from display` option on the taxonomy term field. No idea about your group header. – Finbarr Sep 03 '11 at 16:47
  • great, thanks, dont know how I missed the exlude from display box, soz about that. Thats all working a treat now :) – Purplemonkey Sep 03 '11 at 18:55