This can be done by providing a custom theme function for theming all links. In your theme's template.php
file, create this custom theme (don't forget to update the function name):
/**
* Overrides theme_link() to custom classes.
*/
function arch_link($vars) {
// static counter to track the number of links on the page
$counter = &drupal_static(__FUNCTION__);
if (is_null($counter)) {
$counter = 0;
}
// get a backtrace of all function calls leading to this point
$bt = debug_backtrace();
// try to identify a views file in the backtrace
$is_views = FALSE;
foreach ($bt as &$item) {
if (isset($item['file']) && strpos($item['file'], 'views/views.module') !== FALSE) {
$is_views = TRUE;
$counter++;
break;
}
}
// add our unique class
if ($is_views) {
$vars['options']['attributes']['class'][] = 'views-' . $counter;
}
}
Reliably identifying a link that's generated by the Views module is very hard. The only immediate thing I can think of is checking for a Views file in the backtrace callback, which is what the code above does. This does not guarantee that the link is from Views, but it does mean that Views was at least involved in the process of getting to that link. Also, theme_link()
gets called for every link that passed through Drupal's l()
function so you want to be really careful about not doing any slow operations here. Doing the check for the Views file above is a bad idea (performance-wise). I would remove that entire chunk of code, and just go with a unique class for every link on the page - I think that should work for your needs.