Hi All,

I was looking for a way to display several Logos and add to each of these a web link.
I created a content type called LogoLinks who contains a logo image, a description, a url and is "categorised" into a taxonomy.
I used the bonuspack to create a grid view of logos which belong to certain term in the taxonomy.
This view "feeds" a block which I place on certain pages on the site.

What I would like to achieve is that the grid displays the logos of each link, on mouse over there is the description that appears and on click a new browser page opens with the target url for that logo.

I started to look at theming the specific node-LogoLinks.tpl.php but did not cange anything.
Then I tried to use the theme wizard of the view module but there again the tree produced files:
- template.php
- views-list-permanent_bar.tpl.php
- views-list-permanent_bar.css

Changes to the - views-list-permanent_bar.tpl.php did not seem to be reflected on the website.

How can I theme the contents of my Grid View?
What am I doing wrong?

Thanks
Thefunk

Comments

bao truong’s picture

Component: Documentation » Grid View
Priority: Normal » Critical

I completely agree! How do we theme the grid view? Can't seem to do it using the Theme Wizard. I even used the following function and tpl file all of which were generated by the Theme Wizard.

Here is the name of the tpl file generated, views-list-VIEW_NAME.tpl.php

Here is the function the theme wizard told me to use:

function THEME_NAME_views_view_list_VIEW_NAME{($view, $nodes, $type) {
  $fields = _views_get_fields();

  $taken = array();

  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  // Set up some variables that won't change.
  $base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  foreach ($nodes as $i => $node) {
    $vars = $base_vars;
    $vars['node'] = $node;
    $vars['count'] = $i;
    $vars['stripe'] = $i % 2 ? 'even' : 'odd';
    foreach ($view->field as $id => $field) {
      $name = $field_names[$id];
      $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      if (isset($field['label'])) {
        $vars[$name . '_label'] = $field['label'];
      }
    }
    $items[] = _phptemplate_callback('views-list-VIEW_NAME', $vars);
  }
  if ($items) {
    return theme('item_list', $items);
  }
}

and here is the theme function that I pulled out of the bonus grid view module

function THEME_NAME_views_bonus_view_grid_VIEW_NAME($view, $nodes, $type) {
  drupal_add_css(drupal_get_path('module', 'views_bonus_grid') .'/views_bonus.css');
  $fields = _views_get_fields();
  $content = '<table class="view-grid view-grid-' . $view->name . '">';
  
  // set default count.
  $cols = $view->gridcount ? $view->gridcount : 4;

  $count = 0;
  $total = count($nodes);
  foreach ($nodes as $node) {
    $item = '';
    if ($count % $cols == 0) { 
      $content .= '<tr>'; 
    }

    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        if ($field['label']) {
          $item .= "<div class='view-label view-label-$field[queryname]'>" . $field['label'] . "</div>";
        }
        $item .= "<div class='view-field view-data-$field[queryname]'>" . views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) . "</div>";
      }
    }
    $content .= "<td class='view-grid-item'><div class='view-item view-item-$view->name'>$item</div></td>\n"; 

    $count++;
    if ($count % $cols == 0 || $count == $total) {
      $content .= '</tr>';
    }
  }
  $content .= '</table>';
  
  if ($content) {
    return $content;
  }
}

The put both these babies in my template.php file and then modified the tpl file to see if changes in tpl will be reflected in the grid view. Any changes in the tpl file were reflected only when I converted the view to list view but when I selected bonus grid view, the changes in the tpl file are not reflected. I hope this makes sense. Please provide us with some guidance and tell us what we might be doing wrong and how to make it right. Thank you.

thefunk’s picture

Hi Bao,

Sorry for the late reply.
In the end I gave up the use of the wizard to do this job. I got a friend who knows how to work in PHP to come over and help me.
I have no idea about all the details but ...

All grid views are configured in the same way with a number of columns limited to 5. (in my case it works but I would like to remove this constraint)

Here is the code which is placed in the template.php
The HTML formatting is in the views-grid-permanent_bar tpl.php file and is a basic Clickable Image with On mouse over Title

/* This themes the GRID view but all grid views are limited to 
 * have the same anount of items in a single row
 * they also all go to the  views-grid-permanent_bar tpl.phpfor further instructions
 * This is now used for LOGOS in the permanent bar and the SONG Logos in the SOngs GRid 
*/ 
  
function theme_views_bonus_view_grid($view, $nodes, $type) {
  drupal_add_css(drupal_get_path('module', 'views_bonus_grid') .'/views_bonus.css');
  $fields = _views_get_fields();
  
  // Set up the fields in nicely named chunks.
  foreach ($view->field as $id => $field) {
    $field_name = $field['field'];
    if (isset($taken[$field_name])) {
      $field_name = $field['queryname'];
    }
    $taken[$field_name] = true;
    $field_names[$id] = $field_name;
  }

  $content = '<table class="view-grid view-grid-' . $view->name . '">';
  
  // set default count.
  $cols = $view->gridcount ? $view->gridcount :5;

  $count = 0;
  $total = count($nodes);
  foreach ($nodes as $node) {
    $item = '';
    if ($count % $cols == 0) { 
      $content .= '<tr>'; 
    }
}

$base_vars = array(
    'view' => $view,
    'view_type' => $type,
  );

  
  //Loops through the nodes coming from the view query.
  foreach ($nodes as $i => $node) {
    $nodecounter = 0;
    $fieldcounter = 0;
    
    $nodecounter ++;
    $vars = $base_vars;
    $vars['node'] = $node;
    $vars['count'] = $i;
    $vars['stripe'] = $i % 2 ? 'even' : 'odd';
    foreach ($view->field as $id => $field) {
    $fieldcounter++;
      $name = $field_names[$id];
      $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
      if (isset($field['label'])) {
        $vars[$name . '_label'] = $field['label'];
        }
      }
      $item = _phptemplate_callback('views-grid-permanent_bar', $vars);
      $content .= "<td class='view-grid-item'><div class='view-item view-item-$view->name'>$item</div></td>\n";
      $count++;
      if ($count % $cols == 0 || $count == $total) {
        $content .= '</tr>';
      }
    }
    //$content .= "<td class='view-grid-item'><div class='view-item view-item-$view->name'>$item</div></td>\n"; 

    
  
  $content .= '</table>';
  
  if ($content) {
    return $content;
  }
}

?>

The number of columns you can easily change in the above file.
Hope this helps

TheFunk

thomasmuirhead’s picture

you can change the number of columns easily in the view:

Obviously, you have to have selected View Type: Bonus: Grid View

Then in Arguments open Argument Handling Code and put in: $view->gridcount = 5;

whatever number you use your view will have that number of columns...so the above code gives you 5 columns.

You can then control the number of posts so say 20 will give you 4 rows of 5, whereas if you put 21 you'll get 4 nice full rows and one extra on it's own...so just be careful with that.

This means you can change each grid view rather than all your grid views having the same number of columns

I hope that's helpful.

manuel garcia’s picture

that was very helpful thomasmuirhead! - thanks

manuel garcia’s picture

Thanks thefunk!

To those looking to use it:
The function seems to be working fine, check the line where it says:

$item = _phptemplate_callback('views-grid-permanent_bar', $vars);

and place in there the name of your template file that you will be using for all your grid views, in my case used view-grid-grid.tpl.php, so i put in there 'views-grid-grid'.

Keep in mind that this file doesn't exist yet, and you will have to create it from scratch. I recommend using the following to see what you have in your view to play with:

print '<pre>';
print_r ($view)
print '</pre>';

Then start printing the fields as you wish. Keep in mind that different views might have differently named objects with the content in it...

This is definitely a good start for theming grid views. Perhaps we should write up a decent documentation page with it. I suppose it should be double checked first by people who know about this (developers), because i don't understand the whole process yet. View template files and variables still puzzle me a bit...

Again, thanks for the function - hope this helps.

ow1n’s picture

One correction: When I tried that code I was getting invalid markup, with a huge line of "<tr><tr><tr>" at the start of the table. Moving

    if ($count % $cols == 0) {
      $content .= "\n<tr>";
    }

Inside this loop:

  //Loops through the nodes coming from the view query.
  foreach ($nodes as $i => $node) {

...fixed the problem.

manuel garcia’s picture

I didnt even notice that, good call ow1n , and yes, I confirm your solution does fix the problem :) - thanks!

aelling’s picture

I am also trying to theme a grid view but I am having some difficulty. I am trying to alternate a different background color for each <td> tag that contains a node, but I cannot figure out how to do it. I am fairly new to PHP and Drupal so is there a way I could adapt the above function to display what I am looking for?

tannerjfco’s picture

Thanks for the code, thefunk, it's looks like it's just about what I'm in need of. One question though: It looks as though this code will override theming for all grid views-- I need it to override only one grid view, as this view will have different formatting from the other grid views I'll have on the site. How would I modify that code to accomplish this?

Regards,
Tanner

neclimdul’s picture

Check the theming functions section on http://drupal.org/node/42597. This page has a lot information on themeing views and it all applies to the style plugins provided by views_bonus.

Hope that helps.

ferrangil’s picture

Anyone theming Grid Views on Views 2?

neclimdul’s picture

This issue is related to Views 1 and Views Bonus Pack 1.2 for Views 1. Theming grid in Views2 is a Views issue since it isn't part of the bonus pack anymore.

doomed’s picture

I wish there was a "bonus" pack with lots and lots of nifty grid / table views for Drupal 6.

They're so useful but not so easy to theme.

neclimdul’s picture

Status: Active » Closed (fixed)