I have a module, called "bundles", there I created a file "attached_email.tpl.php" and a hook_theme:

function bundles_theme($existing, $type, $theme, $path)
    $path = drupal_get_path('module', 'bundles');

    return array(
        'attached_email' => array(
            'template' => 'attached_email',
            'path' => $path,

I have 2 databases for this website: lwt (default) and lwt_test (for test data). I have written the template, cleared caches and imported test data into lwt_test DB. But the template is unavailable there. When I call:

theme('attached_email', $data);

From lwt database, it gives the output. But when I switch to lwt_test:


nothing is given. I can only conclude, that there is some piece of data, stored in lwt database, missing in lwt_test database. And that's why Drupal doesn't see my new template.


When I use this code:

print theme('attached_email');
send_attached_emails($bundle_id, $users);

It gives the correct result (probably the template is cached after the 1-st call).

  • Try to delete 'path'=> $path. Drupal already knows the template is in your module folder.
    – Robin
    Commented Feb 27, 2015 at 14:36
  • @Robin Thx, it simplified the hook_theme function, but didn't fix the issue.
    – user4035
    Commented Feb 27, 2015 at 14:45
  • Try to add another theme like "attached_email_test" and render that template after you used db_set_active('lwt_test');. If this template outputs data, you can probably asume that the previous template gets cached
    – Robin
    Commented Feb 27, 2015 at 14:54
  • @Robin I created a new template "attached_email_test" with a new file for it. And called it after calling theme('attached_email');. Surprisingly, it worked. Now the first template also works. But it didn't. What on Earth could it be? cleared the cache before, but it didn't work.
    – user4035
    Commented Feb 27, 2015 at 15:02
  • So if you add the second template everything works, if you use only one template, the second time it is blank?
    – Robin
    Commented Feb 27, 2015 at 15:04

1 Answer 1


Normally, db_set_active() is used to query external data. I have never heard about it being used to switch between two full Drupal databases from within the same active codebase / website.

I suspect one or two things is happening: you are having cache problems and/or you are having registry problems.

When you clear caches via the UI or drush, the full registry gets rebuilt, which includes theme functions and their template files. That information will get stored in whatever database is active at the time.

If you switch databases, the cache and registry will be pulled from the newly active database. In addition, any new cache entries will be written to the new database.

So, I think your problem is that the wrong cache and/or the wrong registry is being queried when you use the Drupal API and you are getting stale data.

The proper way to accomplish this is to set up a dev/staging/live environment, where code goes forward from dev -> staging -> live, and databases go backwards from live -> staging -> dev. There are lots of articles on the net, and a few questions here, about how to do this.

  • Can you provide a couple of links to such articles, related to Drupal: I don't know, what request to google.
    – user4035
    Commented Feb 27, 2015 at 15:45
  • @user4035 Not sure if I have anything bookmarked. I use drush sql-sync and drush rsync to get the database and files backwards from live to dev, and then a normal git workflow to move code from dev to live. Googling for 'drupal staging live workflow' may be a good start.
    – mpdonadio
    Commented Feb 27, 2015 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.