Hi,

Is it possible to use several instances of basically the same function inside template.php

Like, I have a function for a bonus view grid:

function channel_nine_views_bonus_view_grid($view, $nodes, $type) {
  drupal_add_css(drupal_get_path('module', 'views_bonus') .'/views_bonus.css');
  $fields = _views_get_fields();
  $content = '<table class="view-grid view-grid-' . $view->name . '">';
  $count = 0;
  $total = count($nodes);
  foreach ($nodes as $node) {
    $item = '';
    if ($count % 2 == 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 % 2 == 0 || $count == $total) {
      $content .= '</tr>';
    }
  }
  $content .= '</table>';
  
  if ($content) {
    return $content;
  }
}

Now, I would like to use that function only that I want my grid to be with let's say 5 columns... I want to use it for a different view... How do I do that? Do I make a new function inside template.php with different name than original?

k.

Comments

Darko’s picture

you just need to name each function with that view's name i.e.
function YOUR_TEMPLATE_NAME_views_bonus_view_grid_YOUR_VIEW_NAME

bao truong’s picture

Hi Darko,

I was happy to have stumbled across your response and wanted to seek further insight from you on bonus grid view theming. How do we theme the grid view with a tpl file? 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. Looking forward to hearing back from you. Thank you.

Dimegga’s picture

I Believe the theme wizard only works for "List Views" as of now.

if you take a look of the filename "views-list-VIEW_NAME.tpl.php"

you can see it is for list views.

jenlampton’s picture

Yes that's correct, The Wizard only works for list type views. However, you can create your own .tpl.php file for the grid view by placing a call to a phptemplate function in your template.php, passing along the variables you need, and returning your .tpl.php file. I've done the same thing with table views and it's actually quite simple.

Jen

*~ current projects: www.intowine.com, www.researchsolar.com, www.dogstardaily.com ~*

socialnicheguru’s picture

chris

http://SocialNicheGuru.com
Delivering inSITE(TM), we empower you to deliver the right product and the right message to the right NICHE at the right time across all product, marketing, and sales channels.

RAFA3L’s picture

Please, can you show here an example of your code?

I would like style each field and load the images from diferent sources, depend of the theme selected by the user.

thanks in advance

blup’s picture

I've been having the same problem for a while but got nowhere. I tried combining both functions into one, but had no success. Here's my progress so far, in case it helps anyone: http://drupal.org/node/339760