0

I have a custom theme hook that I have a tpl for but my theme suggestions are not working. It appears that they are not even being looked for. I am guessing that I am missing something.

Code:

function MYMODULE_theme() {
  module_load_include('inc', 'mymodule', 'theme/mymodule.theme');
  return mymodule_theme_theme();
}

function MYMODULE_theme_theme() {
  $path = drupal_get_path('module', 'MYMODULE') . '/theme';

  return array(
    'mymodule_theme' => array(
      'variables' => array('module' => NULL, 'key' => NULL, 'body' => NULL),
      'template' => 'mymodule-theme',
      'pattern' => 'mymodule_theme__',
      'file' => 'mymodule.theme.inc',
      'mail theme' => TRUE,
      'path' => $path,
    )
  );
}

function MYMODULE_preprocess_mymodule_theme(&$vars) {
  $module = $variables['module'] ? $variables['module'] : basename(__FILE__, '.theme.inc');
  $functions = array();
  $variables['theme_hook_suggestions'][] = $functions[] = 'MYMODULE_theme__' . $module;
  if (isset($variables['key'])) {
  $variables['theme_hook_suggestions'][] = $functions[] = 'MYMODULE_theme__' . $module . '__' . $variables['key'];
  }
  foreach ($functions as $function) {
    $function = "MYMODULE_preprocess_$function";
    if (function_exists($function)) {
      $function($variables);
    }
  }
}

The theme hook suggestions are being applied as the new preprocess function works.

function MYMODULE_preprocess_MYMODULE_theme__MODULE__KEY(&$vars) {
  dpm($vars);
}

$vars gets printed.

However my MYMODULE-theme--MYMODULE--KEY.tpl.php is not being called.

The base MYMODULE-theme.tpl.php does work. Theme debug output:

<!-- THEME DEBUG -->
<!-- CALL: theme('MYMODULE-theme') -->
<!-- FILE NAME SUGGESTIONS:
   * MYMODULE-theme--MYMODULE--KEY.tpl.php
   * MYMODULE-theme--MYMODULE.tpl.php
   x MYMODULE-theme.tpl.php
-->

Any ideas?

EDIT: I forgot the calls I have tried. Here they are:

$output = theme('MYMODULE_theme', array('body' => $body, 'key' => $key));

and I even tried:

$output = theme(array('MYMODULE_theme', 'MYMODULE_theme__MYMODULE__KEY), array('body' => $body, 'key' => $key));
0

Have you tried making the template name and pattern in the hook_theme implementation uppercase like this:

  'template' => 'MYMODULE-theme',
  'pattern' => 'MYMODULE_theme__',

You can also try to remove the pattern completely. It doesn't seem to be necessary at least in the example use case.

Preprocess functions for theme suggestions doesn't work in Drupal 7, and that's why you are manually calling them in your preprocess function. They are being added to the array so they are called but they are not in the correct format so that theme system could handle them.

  • Finally got back to this task. Removing the pattern worked for me. Thanks a ton. Not sure why they have it. – sareed Dec 15 '15 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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