1

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">
        Active
    </div>
</div>

I'd like to conditionally add classes:

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

1 Answer 1

2

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


UPDATE

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.

3
  • I need the class to be conditional depending on something out of scope
    – SMTF
    Mar 28, 2015 at 0:11
  • I updated the answer
    – M a m a D
    Mar 28, 2015 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, 2015 at 18:38

Your Answer

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

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