Editing HTML around Displayed View Fields: Views Theme Wizard

Question #2 also comes to us via our Facebook forum

Modifying existing views.

Michael Blake (Orlando, FL) wrote on Jan 24, 2008 at 12:08 PM.

I'm working on an existing Drupal application that has a custom Content Type. This includes some custom fields. When I change a custom field, such as add markup before and after it, This does not appear in my views. I think it's because my Views have been exported.

However if I change the .php files in the themes directory directly on the server, I get what I want. My question is , if I go about it this way, can someone else come along export a view and erase my changes? Or do I need to re-export every time I make a change?

Thanks.

Hi Mike,

It looks like someone has used the "Theme Wizard" utility you see at the top of the admin/build/views page. This is a very handy utility that allows a webmaster to have complete control over the individual <li>'s of "List" type views.

You don't need to re-export anything, but if you change the id of the view you will need to add some code to your template.php file, but I'm getting ahead of myself.

To enable the Wizard, one must select the view in question (even default system views are available, like "front page" or "tracker" for example). After you find the view you want to edit, you must select a type. I recommend "Simple List" for 95% of all uses.

Now you are presented with 3 boxes, one containing the HTML for your view in standard Drupal PHPTemplate syntax, which gets copid into a file called views-list-something.tpl.php in your current theme's folder. If you have a theme with multiple "flavors" like minelli or bluebreeze, put this file in the root folder instead of the child folder in case you ever switch from fixed-width to variable.
Particularly useful is a commented list at the top of this page containing all the available fields, which saves you from most print_r() calls. The first thing I normally do with this file is delete all the labels, since that's not usually much fun for usability.
Note: this code gets looped over on every single row (so if you have 10 items per page, this code is run 10 times, keep that in mind).

Second is a handy CSS file, which I consider to be optional, but they've already included it in the .TPL file, so copy it over for ease of use. Better still, create a named section at the bottom of your main style.css and copy this code in there, now your users have one less file to load when they hit your site.

Finally and most important is a function you must drop into your template.php file. I highly recommend putting all the view-generating codes at the bottom of the list. You may even see some developers consolidating all of their phptemplate_views_view_list_something() functions into one function with an extra argument, with a few stub functions to call the master, since 99% of this code is repeated every time you copy it in. The important line here starts with:

$items[] = _phptemplate_callback(...)

If you end up mangling any of these files too much, you can always re-generate the template code in the wizard (AKA the fire where the ring was forged).

I hope that answers 110% of your questions about Views and PHPTemplate files. If you need any more help, reply on Facebook or use the "Ask Us" form at the top of this site

Comments

Using your info as a starting point, we came up with this, which may be helpful to somebody:

I'm working on a site that uses a specific 'views-list-whatever.tpl.php' for all views (6 of them in all). We came up with this modification of the code generated by the Theme Wizard for the template.php file that will override the default theming for all views and use the 'views-list-whatever.tpl.php' for each one. The point of doing this was to avoid having to repeat essentially the same chunk of code 6 times in template.php. This changes the code generated by Theme Wizard in 2 places:

1. In the first line, the name of the view is removed from the function name, leaving:
function phptemplate_views_view_list($view, $nodes, $type) {

2. In the phptemplate_callback function toward the end, the view name is generated using $view->name, like this:
$items[] = _phptemplate_callback('views-list-'. $view->name, $vars);

Like I said, this is only useful if you're doing custom theming on all views, but you might be able to modify it to work only on specific views by adding a series of "if" conditionals, ie:
if ($view->name == 'whatever') {
$items[] = _phptemplate_callback('views-list-whatever', $vars);
}

Anyway, here's the whole thing:

function phptemplate_views_view_list($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);
}
}

Submitted by kittinfish (not verified) on Mon, 06/30/2008 - 13:37

Hi,

i have to install Drupal 6 and i can't use phptemplate_view... in drupal 5 everything works perfectly (i use wizard )

could you help me?

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name