0

I'm using views to output a list of links (fields). I can type in an url and a text and the text becomes the link text that goes to the url. Now I want to be able to add a unique class to the link when I add it from the backend. Or have a unique class/ID added to it by default, that is also fine.

How can I do this?

1

Go to Theme information link in your view, create a new

 views-view-fields.tpl.php

http://api.drupal.org/api/views/theme!views-view-fields.tpl.php/7

there you could assign a dynamic value from your query result to $field->class or add another custom wrapper to each field or use http://api.drupal.org/api/views/theme!views-view-field.tpl.php/7

for this field only

1

According to this post, it is a good idea not to use templates for individual fields for performance reasons. I also found this very helpful post on how to change the output of a views field dynamically. These posts are older, but I think they are still valid.

Now for the problem at hand. I was able to change the class(es) used on a view link field as follows (with help from the above post):

function mytheme_views_view_field__myview__mydisplay__myfield(&$vars) {

    $path = parse_url($vars['field']->last_tokens['[field_foo]'])['path'];
    $path = drupal_get_normal_path(substr($path, 1));

    if ($path == $_GET ['q']) {

        $vars['field']->options['alter']['link_class'] = 'foo_1_class';

    } else {

        $vars['field']->options['alter']['link_class'] = 'foo_2_class';

    }

    return $vars['field']->advanced_render($vars['row']);
}
0

If you click on the Field (in the view), go to STYLE SETTINGS. Here you can add classes and all that good stuff.

  • Yes but that class is a common class for all those links. I wan't to be able to add a unique ID or class to each link I put in there. – Johan Dahl Feb 28 '13 at 16:04
  • You can always use drupal.org/project/views_php to create your own rows (although the custom view (the one Pan Chrono suggests) would be a better solution) – TheEdonian Feb 28 '13 at 16:21
  • Do not use views_php. shrug This answer is for most cases the best one in my opinion, but it needs to be added that the style settings can accept tokens. (Such an edit is already in the queue) If this answer hadn't been very good, I'd have down voted it on the views_php suggestion, which is the complete opposite of right. – Letharion Mar 3 '13 at 10:37
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.