I am using Drupal 6.x and Views2. I am trying to theme the view to add a "sticky" class (so I can give the whole row a background-image to indicate it's stickiness). I already have created a views-view-unformatted--my-view.tpl.php file, and have added a class to the wrapper. I need to add an additional class if the node is sticky.
I have no idea how to go about this. In the .tpl.php file that I have now, $row is already-formatted HTML. I have no idea how to access the node's sticky attribute from inside this file.
I added the node's stickiness as a field for the view (excluded from display). I also have successfully dipped into a preprocess function for the view thanks to http://www.drupaler.co.uk/blog/theming-views-drupal-6x/67. Unfortunately, I just don't know how that's useful!
I'm an advanced user (themes, module development, etc) who is just new to Drupal 6 and Views 2. Any help is MUCH appreciated!
Comments
I've been struggling with
I've been struggling with the same issue all morning. Have you discovered any solutions yet?
––
Jeremy Stoller
Senior Graphic Artist
California Science Center
––
Jeremy Stoller
Senior Graphic Artist
California Science Center
I think I've got it!Add the
I think I've got it!Add the following to your template.php file:
You also need to make sure there's a views-view-list.tpl.php template in your theme, otherwise Drupal won't recognize this function. This of course assumes you are trying to style list view.
––
Jeremy Stoller
Senior Graphic Artist
California Science Center
––
Jeremy Stoller
Senior Graphic Artist
California Science Center
Thank you!
Brilliant! Been playing with this preprocessor all morning without much luck. Added a node-unpublished class based on node_status in the same way.
In D5/Views 1, this could have happened by overriding theme_views_view_list in template.php
Just added that so others might possibly find this post when searching theme_views_view_list()
Thanks much!
Thanks Jeremy, also saved me
Thanks Jeremy, also saved me some time figuring this out. Here's a condensed version that doesn't overwrite default styles:
Note that this is for the unformatted style. If you're using a list style just change the function name to phptemplate_preprocess_views_view_list and make sure you have the correct template file in your theme's directory so the preprocess function will work.
Thanks for this, saved me
Thanks for this, saved me some time. I have no clue why this isn't implemented in views anyway, seems like such a common thing.
One thing I did notice, you dropped the views-row class when you're building your classes. May cause issues for some that theme using that class (as I do).
Drupal 7 / Views 3 version
For Views 3 in Drupal 7 use the following code in template.php.
Also don't forget to add sticky as a field to the view (and exclude the field from display).
KOBA - Drupal Webdesign & Webdevelopment
this can be done from the user interface in views 3
In views 3, template overrides like this shouldn't be necessary, as you can add custom classes to rows from the user interface.
You'll need to add the sticky field into the view - I formatted my sticky field like this:
Take a look at the replacement patterns available in one of the fields after the sticky field - mine looked like this:
The replacement pattern for the sticky field is [sticky] - unless your setup is very different to mine, yours will probably be [sticky] aswell.
Type the replacement pattern into the row class field, apply to your display & save the view.
Any sticky nodes should now get the Sticky class.
Nice idea but has a drawback:
Nice idea but has a drawback: on a multilanguage site, the class name will be localized (and changing depending on the language).
Possible to define classname
After setting the Output format to Sticky/ you then scroll down to Rewrite results and check Rewrite the output of this field; you can define the classname as the text.
Doesn't work in views 7.x-3.4 (found solution)
Due to a change in views 7.x-3.4: Add an outut format for "Promoted" similar to what "Sticky" and "Published" have. The sticky output format now outputs on a negative result. So you get the defined classname regardless of sticky status.
(It still doesn't work if you don't use rewrite results, as you get the classes "Sticky" and "Not Sticky" -- which in CSS would both be regarded as sticky.)
A solution is to change the Output format to True/False, then include the token *in* the Rewrite results e.g., sticky-row-[sticky]
This way you'd then get the classes: sticky-row-True and sticky-row-False.
Easier solution!
Instead of choosing “Sticky/Not Sticky” or “True/False” or some such, simply choose “Custom” instead from the drop-down menu.
Then, enter “sticky” (for consistency with other class names) or “Sticky” (if you want to quickly spot it in an HTML Source view) as the Custom Value for TRUE, and leave Custom Value for FALSE empty!
Then, do everything else as described in mshepherd’s outstanding post above. Voila!
This way, you can also give a more semantically meaningful class name if you prefer. For instance, if you’re doing this for your “Upcoming Events” Display of an Event Calendar View, and you use Sticky to flag Events that are of special public interest, you could use “public” instead of “sticky” as your class name by simply entering that as the Custom Value for TRUE.
Thanks! Just what I needed. A
Thanks! Just what I needed. A simple solutions without the help of extra code.
Brilliant!
Thanks for the tip. This worked fine for me.