I have a field formatter that adds a CSS class attribute to the item by code like this in viewElements

$item->_attributes = ['class' => ['my-class']];

It works perfectly when the formatter is used on a node display settings. It also works for a view of node teasers. However when this formatter is used in a view of node fields then the CSS class is missing.

I have a workaround which is to enable the view field style setting "Use field template", in which case the CSS class does appear. However I don't actually want to use the field template and my CSS class is not in any way related to the template. Unfortunately this "Use field template" setting has a bug/limitation in that it causes the field label to be displayed even if the view field setting "Create a label" is disabled.

Please can anyone tell me how to get the field formatter CSS class to appear when used in a view of fields?

Your Answer

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

Browse other questions tagged or ask your own question.