I'm trying to add a class to the region div based on a field value.

For example:
Inside my node I have a field where a user can select an option from some radio buttons. I would like to take the value of that field and add it to the classes used on <div class="ds-1col node node-page node-odd view-mode-full clearfix">

I've done this in the past for fields by using the ds_extras module that comes with ds which provides a theme function theme_ds_field_minimal.

It seems like the function that adds classes to the DS regions is ds_entity_variables. At the end it runs an alter drupal_alter('ds_pre_render', $layout_render_array, $context);
I've tried using hook_ds_pre_render_alter but the classes don't seem to be available.

I'm not sure if there is a way to do this now or if a feature / hook needs to be added for this to work. Any help would be greatly appreciated.

#2 ds-1645500-2.patch1.48 KBdawehner


swentel’s picture

Yeah, the $vars aren't passed in, I'm still not sure whether I want to do that or not.

You can also just implement hook_preprocess_node, that's the easiest way to add the class now.

dawehner’s picture

1.48 KB

Well hook_preprocess_node doesn't really work if you do something for all entities.

Here is a patch which adds a last parameter (so existing functions are not affected) and also an adaption for the docs.


    if (!empty($layout['settings']['layout_attributes_merge'])) {
      // Handle classes separately.
      if (isset($vars['attributes_array']['class'])) {
        $vars['classes_array'] += $vars['attributes_array']['class'];
      $vars['layout_attributes'] .= ' ' . drupal_attributes($vars['attributes_array']);

This code doesn't work together with omega 4, because it unsets references, see omega_initialize_attributes() and omega_cleanup_attributes(),
but I'm not sure how to proper implement it here. Maybe it would be possible to detect the reference by comparing the classes?

dawehner’s picture

Status:Active» Needs review

Adapt status

dawehner’s picture

I know that this was needed for a custom project but yeah I have no idea how urgent that was.

aspilicious’s picture

Status:Needs review» Reviewed & tested by the community

I thought about this for a while and I'm going to do this. It doesn't hurt...
Marking rtbc so I don't forget...

aspilicious’s picture

Version:7.x-2.x-dev» 8.x-2.x-dev
Issue summary:View changes
Status:Reviewed & tested by the community» Patch (to be ported)

Fixed, needs porting to D8.

aspilicious’s picture

Status:Patch (to be ported)» Fixed


aspilicious’s picture

Status:Fixed» Closed (fixed)
bendev’s picture


I did not manage to do this. Could you please detail how you proceed?

Is this compatible with theme omega4?

I have the impression that the hook is not fired.