Add a new region to theme programmatically

Public

Get raw version
php
  1. /**
  2.  * Implements hook_system_info_alter()
  3.  * Adds a new "tray" region to the current theme
  4.  */
  5. function MODULE_system_info_alter(&$info, $file){
  6. global $custom_theme;
  7.  
  8. // If non-default theme configuration has been selected, set the custom theme.
  9. $custom_theme = isset ($theme) ? $theme : variable_get('theme_default', 'bartik');
  10.  
  11. if ($file->name == $custom_theme){
  12. $info['regions'] = array_merge($info['regions'], array('tray' => t('Tray')));
  13. }
  14. }

Comments

fadehelix's picture

I wonder when the use of this hook is better than define a region in .info file ;]

fadehelix's picture

Thanks :) It's good example. I voted up Your answer there ;)

Edit: I have a last question ;) Is there no need to clear cache after adding a region? E.g. by using drupal_flush_all_caches() function ?

arshad's picture

Yep. It's a hook, it does need a clear cache ^^