1

I have created a simple module to display frequently asked questions (FAQ). The output (faq content) can be customize in mymodule-template.tpl.php file located in a module directory.

I'd like to create an additional template file suggestion for the theme directory e.g: sites/all/themes/mytheme/templates/mymodule-template.tpl.php

So, the main goal is to add two template file suggestions.

  1. [my module path]/mymodule-template.tpl.php
  2. [active theme path]/templates/mymodule-template.tpl.php

To achieve #1 I have implemented this hook.

/**
 * Implements hook_theme()
 * @return
 *  An array with the name of template file and variables
 */
function simple_faq_theme($existing, $type, $theme, $path) {
    // get faq content
    $records     = get_faq_content();   // categories, questions, answers
    $description = get_faq_info('intro');   // faq description

    return array(
        'faq_template'  => array(
          'template'    => 'simple_faq',
          'variables'   => array('records' => $records, 'description' => $description),
        )
    );
}


/**
 * FAQ Page content
 *
 * @return
 *  HTML created by template file
 */ 
function simple_faq_page() {
    return theme('faq_template');
}

It works, but I can't figure out how to create an additional suggestion, which allows users to theme the output without changing anything within module files.

Any idea?

  • 1
    So your template file is simple_faq.tpl.php? It should works out of the box, try to copy this file to theme directory. To add suggestions, use template_preprocess_faq_template(&$variables) and add it there like $variables['theme_hook_suggestions'] = array('simple_faq__' . current_path()); – malcolm Apr 21 '13 at 16:01
  • Yes, and it works, but is there any way to do that in module? without preprocess functions? – Jack-PL Apr 21 '13 at 16:18
  • This function should be in your module if You want provide template suggestions, like any other modules do, look eg. to field, node, etc., all of these have function template_preprocess_theme – malcolm Apr 21 '13 at 16:26
  • Copy your simple_faq.tpl.php file to current theme's templates folder and it should work. – AyeshK Apr 21 '13 at 16:44
  • 1
    yes, suggestion is: simple_fag__faq so the filename should be: simple-faq--faq.tpl.php - I've tried with underscores as well, but with no results. Well, at least I'm on the right way. – Jack-PL Apr 21 '13 at 19:32
2

Your hook_theme implementation is not being done in the usual way. It should only have default values for the variables, like this:

/**
 * Implements hook_theme()
 * @return
 *  An array with the name of template file and variables
 */
function simple_faq_theme($existing, $type, $theme, $path) {
  return array(
    'faq_template' => array(
      'template' => 'simple_faq',
      'variables' => array('records' => array(), 'description' => ''),
    )
  );
}

Then when you call the theme function is where you send the actual values in, like this:

/**
 * FAQ Page content
 *
 * @return
 *  HTML created by template file
 */ 
 function simple_faq_page() {
   // Get faq content.
   // Categories, questions, answers.
   $records = get_faq_content();
   // faq description.
   $description = get_faq_info('intro');
   return theme('faq_template', array('records' => $records, 'description' => $description));
 }

As far as I know you can't give 2 values for the template file, however what you want to do should already be possible. All you have to do is tell it where the original template file is in your module, then drupal will automatically find ones that are in any themes (the defining module doesn't have to set up anything for the theme).

  • I've tried to learn why the variables defined in hook_theme() should contain only default NULL values, but I haven't found any explanation about that. I tried to define them once, in hook_theme(), and they works, however, it seems the right way to do that is exactly like you wrote. Thank you very much. – Jack-PL Apr 22 '13 at 12:52
  • The reason is that they are just default values that are used in the case that no value is passed in when calling theme(). If these default values weren't there then there would be lots of undefined index/variable errors if you omitted any variables when calling theme(). This means that theme functions don't need to check for the existence of the variable, they can just rely on the fact it will be there, even if it is just the default value. – rooby Apr 22 '13 at 12:57

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.