Hi every body, I am developing a custom contact us module. I have already build a info file and a template file along with this module. I have also enabled this module from the module list section. Now i have a problem while theming this form. The form is displayed at "contactme/contact" url, But the the function contactme_form_theme is not called also the tpl file is not called.I also tried changing the theme function name as hook_theme this too failed. Can you please help me solve this problem and suggest me how to theme a custom form module.
I also tried to call the theme function from the hook_form function as return theme_contactme_form($form). but i got the following error:
Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\drupal-dev\includes\form.inc on line 984
<?php
// $Id$
/**
* @file
* Module for contactme form.
* This module provides block form for contactme
* **/
/**
* Implementation of hook_block()
*/
function contactme_help($path, $arg){
switch ($path) {
case "admin/help/contactme_help":
$output = '<p>'. t("contactme is a contact module for contactme") .'</p>';
break;
}
return $output;
} // function contactme_help
/**
* Valid permissions for this module
* @return array An array of valid permissions for the contactme module
*/
function contactme_perm(){
return array('administer contactme', 'access contactme contact us form');
} // function contactme_perm()
/**
* Menu for this module
* @return array An array with this module's settings.
*/
function contactme_menu(){
$items['contactme/contact'] = array(
'title' => 'Contact contactme',
'page callback' => 'contactme_message',
'access arguments' => array('Contact contactme'),
'type' => MENU_LOCAL_TASK
);
return $items;
}
/**
* Test Module Messages
* @return array An array of form data.
*/
function contactme_message(){
return drupal_get_form('contactme_form');
}
/**
* The callback function (form constructor) that creates the HTML form for contactme_message().
* @return form an array of form data.
*/
function contactme_form(){
$form['product_demo'] = array(
'#type' => 'checkbox',
'#title' => t('Yes, I would like to view demo.'),
'#default_value' => '',
);
$form['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#size' => 30,
'#maxlength' => 64,
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#size' => 30,
'#maxlength' => 64,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#size' => 30,
'#maxlength' => 64,
);
$form['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#size' => 30,
'#maxlength' => 64,
);
$period = array(0=>"Select One","a"=>"a","b"=>"b","c"=>"c","d"=>"d","e"=>"e","f"=>"f","g"=>"g","h"=>"h","i"=>"i");
$form['market'] = array(
'#type' => 'select',
'#title' => t('Market'),
'#default_value' =>'',
'#options' => $period,
//'#description' => t('The timer.'),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#size' => 30,
'#maxlength' => 64,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#default_value' => variable_get('message', ''),
'#cols' => 50,
'#rows' => 5,
'#description' => t(""),
);
//Submit button:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Theme function for the contactme form.
*/
function theme_contactme_form($form){
return array(
'contactme-template' => array(
'arguments' => array('product_demo'=>NULL),
'template' => 'contactme-template'
),
);
}
/**
* Form validation for this module's settings
* @param form an array that contains this module's settings
* @param form_state an array that contains this module's settings
*/
function contactme_form_validate($form, &$form_state){
}
/**
* Form submission for user data.
* @param form an array that contains user data
* @param form_state an array that contains user data
*/
function contactme_form_submit($form, &$form_state){
}
Comments
The problem is somewhere in
The problem is somewhere in your form. Delete the form elements one at a time, refreshing the page each time, until the error goes away. Then you will know which form element the problem lies in.
Contact me to contract me for D7 -> D10/11 migrations.
custom contact us module
Hi matwichuk,
Thank you for your suggestion. I deleted each form element and refreshed the page, but still the theme_hook_form is not called.
I just need a simple sample of code for theming a form module.
You haven't told it to use
You haven't told it to use your theme function anywhere.
To create theme functions/templates, you always first need to use hook_theme() to register your theme:
Then you need to either create a template or a theme function. Themeing forms requires a lot of php, so I personally think that a function is better than a template:
To call a theme from a form, you need to call the theme in the form declaration:
$form['#theme'] = 'mytheme';This will automatically pass the variable $form to your theme.
When themeing forms, you will need to call drupal_render() on each of the form elements when theming them, then call drupal_render() on the whole form, returning the themed form.
Contact me to contract me for D7 -> D10/11 migrations.
Thank you very much
Hi Jay,
I cant believe that it worked. Wow thank you very much man. I was having a hard time googling and not finding the right solution. Your help did the trick. I really appreciate your help.