1

In my theme's template.php file, I defined a new theme hook using hook_theme():

function MYTHEME_theme() {
  $hooks = array(
    'custombody' => array(
      'template' => 'custombody',
      'variables' => array(
        'content' => array(
          'body' => '',
        ),
      ),
      'path' => drupal_get_path('theme', 'MYTHEME') . '/templates/custom',
    ),
  );

  return $hooks;
}

I also placed a file named custombody.tpl.php under templates/custom.

But I can't get this template working. It's not found from Drupal. I keep getting this warning in the log:

Theme hook custombody not found

Details:

I am trying to use this template to style the body part of HTML emails. I'm using Swift Mailer to send HTML emails and I want to theme the body part with my custom theme. There is no problem on the swiftmailer part.

Normally swiftmailer.tpl.php prints the body text as it is:

<?php print $body; ?>

I've changed this in my own implementation of swiftmailer.tpl.php in my theme folder like that:

<?php print render($body); ?>

And I'm sending a render array as $body, instead of a string, what normally would be expected.

The array I'm sending is:

$variables['body'] = array(
  '#theme' => 'custombody',
  '#content' => array(
    'body' => 'Actual body text here',
  ),
);

What am I missing, that my custombody template is not being found?

UPDATE:

I also created a theme_custombody() function in my template.php file to test it, but it also doesn't work.

function theme_custombody($variables) {
  return 'TEST';
}

closed as unclear what you're asking by kiamlaluno Nov 1 '15 at 6:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I hate to be the one to ask this but... have you cleared caches? – beth Mar 21 '14 at 16:21
  • Yes, I've cleared all caches multiple times :( – Елин Й. Mar 21 '14 at 20:39
  • The question doesn't explain in which way the theme function is used, nor in which context is invoked. Also, it doesn't show in which way the theme function replaces the one used from the module. There are some missing details that don't allow us to answer the question. – kiamlaluno Nov 1 '15 at 6:09
  • drush dis <module> -y; drush pm-uninstall <module> -y; drush en <module> -y; Worked for me, my hook_theme array key didn't match. I changed it after the module had been enabled. No amount of cleared cache would fix it. Apparently the install process registers the theme stuff. A "drush cc theme-registry" might have fixed it. – SpiesInOrbit Aug 4 '17 at 21:07
0

Try something like this:

function mytheme_preprocess_page(&$variables, $hook) {
  $variables['theme_hook_suggestions'][] = 'custombody';
}

to add a theme hook suggestion a template called 'custombody.tpl.php'.

However it sounds like what you really want to do might be to render the $body directly, before it gets passed to the default template. In that case you'd use a process or preprocess function to do that directly instead of doing it in the template itself.

0

I had no trouble getting your code to work as-is.

I copied and pasted your theme function into my theme, created the template file at path-to-MYTHEME/templates/custom/custombody.tpl.php and cleared caches.

Then I created a test script, script.php and ran it with drush php-script script.php.

<?php
  echo theme('custombody', array('content' => array('body' => 'Hello, World!')));
?>

Some things to try:

  1. Make sure that the path attribute is actually being set to what you expect. Using the PHP filter, a bootstrapped script, a drush script, or somewhere in your code,

    echo drupal_get_path('theme', 'MYTHEME') . '/templates/custom';

  2. Make sure that the name of your theme is correct, no typos, both in the function name and in the path attribute.

  3. Also check that the name of your theme (e.g. MYTHEME) is unique. Make sure you don't have a module using that same key for a name.

  4. Make sure that your theme is enabled.

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