/**
* Make a modul form temable with a x.tpl.php file
*
* ##################################
* Problem: Fatal error after submit!
* ##################################
*
* @see http://drupal.org/node/165706 (http://drupal.org/node/112358 for Drupal 4 and 5)
*/

// $Id$

/**
 * @file
 * /sites/all/modules/modulname/modulname.module
 */

/**
 * Implementation of hook_block().
 */
function modulname_block($op='list', $delta=0) {
  if ($op == "list") {
    $block[0]["info"] = t("Modul Name");
    return $block;
  }
  else if ($op == 'view') {
    if (user_access('access modulname content')) {
    $block['subject'] = t('Modul Name');
    $block['content'] = drupal_get_form('modulname_form');
    return $block;
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function modulname_theme() {
  return array(
    'modulname_form' => array(
      'template' => 'modulname-form',
      'arguments' => array(
        'form' => NULL,
        'modulname_form_value' => NULL,
      )
    ),
  );
}

/**
 * Process variables for modulname-form.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $modulname_form_value: Description.
 *
 * @see /sites/all/modules/modulname/modulname-form.tpl.php
 */
function template_preprocess_modulname_form(&$variables) {

// TODO Note: Code from /modules/search/search.module line 1120

  $variables['modulname'] = array();
  $hidden = array();
  // Provide variables named after form keys so themers can print each element independently.
  foreach (element_children($variables['form']) as $key) {
    $type = $variables['form'][$key]['#type'];
    if ($type == 'hidden' || $type == 'token') {
      $hidden[] = drupal_render($variables['form'][$key]);
    }
    else {
      $variables['modulname'][$key] = drupal_render($variables['form'][$key]);
    }
  }
  // Hidden form elements have no value to themers. No need for separation.
  $variables['modulname']['hidden'] = implode($hidden);
  // Collect all form elements to make it easier to print the whole form.
  $variables['modulname_form'] = implode($variables['modulname']);
  $variables['modulname_form_value'] =  $modulname_form;
}

/**
function template_preprocess_modulname_form(&$variables) {
$modulname_form = variable_get('modulname_form', '2');
$variables['modulname'] = array();
  if ($variables['block']) {
    $variables['template_files'][] = 'modulname-form';
  }
  $variables['modulname_form_value'] =  $modulname_form;
}
*/

/**
 * Implementation of hook_form().
 */
function modulname_form($form) {
  $form['modulname_form'] = array(
    '#type' => 'select',
    '#name' => 'modulname_form',
    '#options' => array(
      '1' => t('One'),
      '2' => t('Two'),
    ),
    '#default_value' => variable_get('modulname_form', '2'),
    '#weight' => 0,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 1,
  );
  $form['#redirect'] = FALSE;
  $form['#skip_duplicate_check'] = TRUE;
  return $form;
}

/**
 * Implementation of hook_form().
 */
function theme_modulname_form($form) {
  $output = drupal_render($form);
  return $output;
}
// $Id$

/**
 * @file
 *   /sites/all/modules/modulname/modulname-form.tpl.php
 */
<div>
  <?php print $modulname_form; 

print $modulname_form_value;

?>

/**
* RESULT
* On submit. Error: "Fatal error: Cannot use string offset as an array in /var/www/drupal6/includes/form.inc on line 977".
*
*/

/**
 * @file
 * /includes/form.inc
 */

// 975  unset($edit);
// 976  if (!empty($form['#disabled'])) {
// 977    $form['#attributes']['disabled'] = 'disabled';
// 978  }

Comments

CZ’s picture

Missing: $form = array();