Adding to $items in a Drupal view

I recently needed to add an extra link to the end of my $items list in a list view and found that this could be done quickly in template.php.

I set out to do two things:
- add a link to the end of my list view that does not contain node data
- as part of this last list item, include a custom link that changes depending on the name of the view

Accomplishing this was a lot easier than I expected. All I had to do was add a switch statement for the view name, and also add a line with the content I wanted to add to my $items array before the theming function.

Here is my code:

  1. <?php
  2. function phptemplate_views_view_list_myview($view, $nodes, $type) {
  3. $fields = _views_get_fields();
  4. $taken = array();
  5.  
  6. // Set up the fields in nicely named chunks.
  7. foreach ($view->field as $id => $field) {
  8. $field_name = $field['field'];
  9. if (isset($taken[$field_name])) {
  10. $field_name = $field['queryname'];
  11. }
  12. $taken[$field_name] = true;
  13. $field_names[$id] = $field_name;
  14. }
  15.  
  16. // Set up some variables that won't change.
  17. $base_vars = array(
  18. 'view' => $view,
  19. 'view_type' => $type,
  20. );
  21.  
  22. foreach ($nodes as $i => $node) {
  23. $vars = $base_vars;
  24. $vars['node'] = $node;
  25. $vars['fullNode'] = node_load($node->nid);
  26. $vars['link'] = l($vars['fullNode']->body, 'node/' . $node->nid);
  27. $vars['count'] = $i;
  28. $vars['stripe'] = $i % 2 ? 'even' : 'odd';
  29.  
  30. //adding extra variable for view name contextual links, in a template, this will be $linked_page.
  31. switch ($vars['view']->name) {
  32. case 'view_one':
  33. $vars['linked_page'] = '/news/special_one';
  34. break;
  35.  
  36. case 'view_two':
  37. $vars['linked_page'] = '/news/special_two';
  38. break;
  39.  
  40. case 'view_three':
  41. $vars['linked_page'] = '/news/special_three';
  42. break;
  43.  
  44. case 'view_four':
  45. $vars['linked_page'] = '/news/special_four';
  46. break;
  47.  
  48. }
  49. foreach ($view->field as $id => $field) {
  50. $name = $field_names[$id];
  51. $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
  52. if (isset($field['label'])) {
  53. $vars[$name . '_label'] = $field['label'];
  54. }
  55. }
  56. $items[] = _phptemplate_callback('views-happy_news', $vars);
  57. }
  58. //the key, I'm adding a link to the $items[] array.
  59. $items[] = "<a href=\"{$vars['linked_page']}\" class=\"more-articles\">See more news</a> »";
  60. if ($items) {
  61. return theme('item_list', $items);
  62. }
  63. }
  64. ?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.

More information about formatting options