There's two basic ways of creating themes from a module - using a theme function, or using a template. In both cases, you need to register the themes in the hook_theme function for your module (pecapture_theme
). That part of your example looks right.
If you register a theme function, you then create the theme function, and call it theme_themename
. If you register a template (like you did) you use an optional template_preprocess function and a template. Template_preprocess functions are called template_preprocess_themename
and are passed, by reference, an array of variables to be then passed to the template. Also, you should never directly call a theme function. Always uses theme('themename', $args)
to access a theme so that drupal can handle it correctly (allows overriding, and correct variable preprocessing).
So, first of all, it looks like you're trying to directly call a theming function that drupal doesn't recognize as theme function. Second, you're using a template, so you need a function called template_preprocess_pecapture_displayform(&$vars)
if you want to process the form before sending it to the template. Also make sure your template is called pecapture-display.tpl.php so it matches exactly the name you supplied in hook_theme except for the extension.
Also, it looks like you're trying to theme a form. So, you'll need to tell drupal_get_form
to use your theme by including $form['#theme'] = 'pecapture_displayform'
in your form function. You can output individual form elements in your theme by calling drupal_render($form['element'])
, calling drupal_render($form)
will render any remaining un-rendered elements (drupal keeps track so they won't be rendered twice).
See http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6 for more information on theming forms.
As long as you've specified the theme in your form function you won't need to call it explicitly, so you should be able to do
$block['content'] = drupal_get_form('pecapture_blockform');
in your block hook.
Note, if you have put your template file in a /theme subfolder in your module dir ( which is best practice), you'll also need to specify the file path in hook_theme
function example_theme($existing, $type, $theme, $path) {
return array(
'example_function' => array(
'variables' => array('var1' => array(), 'var2' => array(), 'var3' => array()),
'template' => 'example-template',
'path' => drupal_get_path('module', 'example').'/theme'
),
);
}
Best Answer