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.

CommentFileSizeAuthor
#2 ds-1645500-2.patch1.48 KBdawehner
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

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

FileSize
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.

Btw.

    if (!empty($layout['settings']['layout_attributes_merge'])) {
      // Handle classes separately.
      if (isset($vars['attributes_array']['class'])) {
        $vars['classes_array'] += $vars['attributes_array']['class'];
        unset($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

Done

aspilicious’s picture

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

Hello,

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.

Thanks

kopeboy’s picture

Version: 8.x-2.x-dev » 7.x-2.x-dev
Status: Closed (fixed) » Needs work

Can we have this on Drupal 7?

  • aspilicious committed 2a5a003 on 8.x-3.x authored by dawehner
    Issue #1645500 by dawehner | gooddesignusa: Add Class to region based on...
aspilicious’s picture

Status: Needs work » Closed (won't fix)

I won't be adding more features to D7.