Both hook_custom_theme()
and hook_init()
are invoked from _drupal_bootstrap_full(), which contains the following code.
menu_set_custom_theme(); // It invokes hook_custom_theme().
drupal_theme_initialize();
module_invoke_all('init');
The reason of the order is explained in the comment before that code, which says:
Prior to invoking hook_init()
, initialize the theme (potentially a custom one for this page), so that:
- Modules with
hook_init()
implementations that call theme()
or theme_get_registry()
don't initialize the incorrect theme.
- The theme can have
hook_*_alter()
implementations affect page building (e.g., hook_form_alter()
, hook_node_view_alter()
, hook_page_alter()
), ahead of when rendering starts.
It seems that your situation is a circular-reference situation, where an implementation of hook_custom_theme()
depends from hook_init()
, and an implementation of hook_init()
could depend from the value returned from hook_custom_theme()
.
If you are sure that the result of hook_custom_theme()
doesn't influence the code of spaces_init()
, which doesn't need the theme to be initialized, then you could invoke spaces_init()
in your hook_custom_theme()
using module_invoke('spaces', 'init')
, but that sounds like a hack, and subject of problems if spaces_init()
is changed, and it actually needs a theme to be initialized before being executed.