How to render a form in Drupal 7 with template.tpl.php?

<?php
// modulname.module

function modulname_permission() {
  return array(
   
'administer_modulname' => array(
     
'title' => t('Administer Modulname'),
     
'description' => t('Administration for Text Size module.'),
    ),
   
'access_modulname_content' => array(
     
'title' => t('Access Modulname content'),
     
'description' => t('Access of Text Size content.'),
    ),
  );
}
function
modulname_block_info() {
 
$blocks['modulname'] = array(
   
'info' => t("Modul name 2")
  );
  return
$blocks;
}
function
modulname_block($op='list', $delta=0) {
  if (
$op == "list") {
   
$block[0]["info"] = t("Modul name 2");
    return
$block;
  }
  elseif (
$op == 'view') {
    if (
user_access('access modulname content')) {
     
$block['subject'] = t('Modul name 2');
     
$block['content'] = drupal_get_form('modulname_form');
      return
$block;
    }
  }
}
function
modulname_block_view($delta) {
 
$block['content'] = drupal_get_form('modulname_form');
  return
$block;
}
function
template_preprocess_modulname_form(&$variables) {
 
$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]);
    }
  }
 
$variables['modulname_form'] = implode($variables['modulname']);
}
function
modulname_form() {
 
$form = array();
 
$form['modulname_select'] = array(
   
'#type' => 'select',
   
'#title' => "Modul name 2",
   
'#name' => 'modulname_select',
   
'#options' => array(
     
'1' => t('One'),
     
'2' => t('Two'),
    ),
   
'#default_value' => variable_get('modulname_select', '1'),
   
'#weight' => 0,
  );
 
$form['modulname_submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Save'),
   
'#weight' => 1,
  );
 
$form['#skip_duplicate_check'] = TRUE;
  return
$form;
}
function
theme_modulname_form($form) {
 
$output = drupal_render($form);
  return
$output;
}

function

modulname_theme() {
  return array(
   
'modulname_form' => array(
     
'variables' => array(
       
'form' => NULL,
      ),
     
'render element' => 'form',
     
'template' => 'modulname-form',
    ),
  );
}

?>
// modulname-form.tpl.php
<?php print $modulname_form; ?>

// Error: Warning: Invalid argument supplied for foreach() in element_children() (line 5043 of /var/www/drupal7/includes/common.inc).

<?php
// Line 5043 in common.inc is: "foreach ($elements as $key => $value) {".

function element_children(&$elements, $sort = FALSE) {
 
// Do not attempt to sort elements which have already been sorted.
 
$sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;

 

// Filter out properties from the element, leaving only children.
 
$children = array();
 
$sortable = FALSE;
  foreach (
$elements as $key => $value) {
    if (
$key[0] !== '#') {
     
$children[$key] = $value;
      if (
is_array($value) && isset($value['#weight'])) {
       
$sortable = TRUE;
      }
    }
  }
 
// Sort the children if necessary.
 
if ($sort && $sortable) {
   
uasort($children, 'element_sort');
   
// Put the sorted children back into $elements in the correct order, to
    // preserve sorting if the same element is passed through
    // element_children() twice.
   
foreach ($children as $key => $child) {
      unset(
$elements[$key]);
     
$elements[$key] = $child;
    }
   
$elements['#sorted'] = TRUE;
  }

  return

array_keys($children);
}
?>

Comments

CZ’s picture

Error found in hook_theme().

maori’s picture

hiya could you tell me what the error in the hook_theme() was

as i'am getting a simmiler error

CZ’s picture