rlhawk

Add first and last classes to regions in Omega subtheme

May 22nd, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. /**
  2.  * Implements hook_alpha_page_structure_alter().
  3.  */
  4. function YOURTHEMENAME_alpha_page_structure_alter(&$vars) {
  5.   foreach (element_children($vars) as $section) {
  6.     if (isset($vars[$section]['#section'])) {
  7.       foreach (element_children($vars[$section]) as $zone) {
  8.         _YOURTHEMENAME_calculate_first_last($vars[$section][$zone]);
  9.       }
  10.     }
  11.   }
  12. }
  13.  
  14. /**
  15.  * Implements hook_alpha_preprocess_region().
  16.  */
  17. function YOURTHEMENAME_alpha_preprocess_region(&$vars) {
  18.   if (!empty($vars['elements']['#grid']['first_column']) && $vars['elements']['#grid']['first_column'] == TRUE) {
  19.     $vars['attributes_array']['class'][] = 'grid-first-column';
  20.   }
  21.   if (!empty($vars['elements']['#grid']['last_column']) && $vars['elements']['#grid']['last_column'] == TRUE) {
  22.     $vars['attributes_array']['class'][] = 'grid-last-column';
  23.   }
  24. }
  25.  
  26. /**
  27.  * Helper function to calculate which regions are positioned first and last
  28.  * in the grid, within a given zone.
  29.  */
  30. function _YOURTHEMENAME_calculate_first_last(&$zone) {
  31.   $regions = array();
  32.   $sort_fields = array();
  33.   $count = 1;
  34.  
  35.   foreach (element_children($zone) as $region) {
  36.     $regions[] = $region;
  37.     $sort_fields['position'][] = (!empty($zone[$region]['#position'])) ? $zone[$region]['#position'] : 0;
  38.     $sort_fields['original_order'][] = $count;
  39.     $count ++;
  40.   }
  41.  
  42.   if (isset($zone['#data']['order']) && $zone['#data']['order'] == TRUE) {
  43.     array_multisort($sort_fields['position'], $sort_fields['original_order'], $regions);
  44.   }
  45.  
  46.   $columns = $zone['#data']['columns'];
  47.   $current_position = 0;
  48.   $previous_region = '';
  49.  
  50.   foreach ($regions as $region) {
  51.     $total_width = $zone[$region]['#grid']['prefix'] + $zone[$region]['#grid']['columns'] + $zone[$region]['#grid']['suffix'];
  52.     if ($current_position == 0) {
  53.       $zone[$region]['#grid']['first_column'] = TRUE;
  54.     }
  55.     else if (!empty($previous_region) && $current_position + $total_width > $columns) {
  56.       $zone[$previous_region]['#grid']['last_column'] = TRUE;
  57.       $zone[$region]['#grid']['first_column'] = TRUE;
  58.       $current_position = 0;
  59.     }
  60.     $current_position += $total_width;
  61.     $previous_region = $region;
  62.   }
  63.   if (!empty($previous_region)) {
  64.     $zone[$previous_region]['#grid']['last_column'] = TRUE;
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment