Using template_preprocess_field doesn't work, I assume, because this is a field from a View, not a node.

I've looked at this answer however that only seems to apply to the field value not the markup and styles surrounding it.

For example: The output of the field in markup is currently:

<div class="views-field views-field-field-status">
    <div class="field-content">

I'd like to conditionally add classes:

<div class="views-field views-field-field-status">
    <div class="field-content my-conditionally-added-class">
  • What type of condition ? e.g?
    – Rupesh
    Mar 27 '15 at 12:57
  • @Rupesh a different value of the node
    – SMTF
    Mar 28 '15 at 0:22

You can add any css class, HTML tag to any field through views admin UI, why don't you add the class from there?


Because you need add the css class based on some condition, you have to use the following preprocess function. your code is going to be some thing like this (maybe a few modification will be needed )

function mytheme_preprocess_views_view_field(&$vars){
     $view = $vars['view'];
    if($YOUR_CUSTOM_CONDITION == TRUE && $view->name == VIEW_NAME)
           $vars['classes_array'] = 'my-conditionally-added-class';

because you output the content on views you have to use mytheme_preprocess_views_view_field function.

For more information visit Themeing Views field output with Preprocess function.

  • I need the class to be conditional depending on something out of scope
    – SMTF
    Mar 28 '15 at 0:11
  • I updated the answer
    – M a m a D
    Mar 28 '15 at 8:37
  • I haven't revisited this as of yet. I mitigated the issue in a less than ideal way but I'll likely look at this again shortly. Cheers.
    – SMTF
    Apr 3 '15 at 18:38

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.