I have a content type article with a field called 'type'. Based on the value of this field I want to display my content type differently in a view that I created with the Views module. In order to do that I would like to add the value of the type field as a css class to each item in that view.

4 Answers 4


The easiest way to achieve this was actually use the views module itself. The value of the type field can be easily used as a row class. Thanks for the other suggestions, but in the end this was all I needed.

enter image description here


You have several solutions, you can create a contextual filter based on type and embed your view with this code views_embed_view('yourview','page',$field['type']) in this way a special view based on this filter will be loaded.

the second solution is to create separate pages for different types, for example page_type1, page_type2 and ... , then in this code views_embed_view use the relevant page based on the type.

I've used both methods and it works


Using the code below you can add class to the fields as per the value of field_type

$form['field_type']['#attributes']['class'] = array(

I believe the Semantic Views module supports tokenized classes. Here is the module description:

This Views plugin makes unformatted styles, field row styles and other output more readily configurable without needing to override template files. Instead of overriding row style templates for views where you want to specify different HTML elements (tags) and class attributes, you can specify these inside the Views UI and avoid overriding templates for each view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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