Php – Drupal Module – Form in Block not applying template

drupalPHP

Help me figure out why isn't my form getting styled a la sites/all/modules/pecapture/themes/pecapture-displayform.tpl.php

Here's my code:

/**
 * Implementation of hook_theme()
 */
function pecapture_theme() {
  $path = drupal_get_path('module', 'pecapture') . '/theme';
  return array(
    'pecapture_displayform' => array(
      'arguments' => array('form' => NULL),
      'template' => 'pecapture-displayform',
      'path' => $path,
    ),
  );
}

Basically this says that theme files are in the module/theme folder ($path)

There is a theme function pecapture_displayform($form = NULL), called using theme('pecapture_displayform', $form) where $form is a Drupal form array

There is a template file pecapture_displayform.tpl.php at $path

function pecapture_block($op = 'list', $delta = 0, $edit = array()) { 
  $block = array();
  if ($op == "list") { // Generate listing of blocks from this module, for the admin/block page
    $block[0]["info"] = t('Persistent E-mail Capture Form Block');
  } 
  else /* if ($op == 'view') */ { // Generate our block content
    $block['subject'] = ''; //'Persistent E-mail Capture Form';
    $block['content'] = pecapture_displayForm();
  }
  return $block;
} // function pecapture_block

This says that when you are viewing the block, use the function pecapture_displayForm() to generate the contents. $block gets php print()ed

/**
* Callback for pecapture_theme 
*/
function pecapture_displayform() {
  return drupal_get_form('pecapture_blockform');
}

This says return the html formatted drupal form array (for output)

function pecapture_blockform(&$form_state) {
  /* the form, standard ... */

This is the form contents, it's typical.

I've tried calling the theme function explicitly in pecapture_displayform:
return theme('pecapture_displayform', $form);
and
return theme('pecapture_displayform', drupal_get_form($form));

So why is the form not going through pecapture-displayform.tpl.php ?

Best Answer

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.

Related Topic