Posted by jsingh on January 7, 2013 at 4:57am
Hi,
I have a custom page which I created in a custom module with hook_menu
$items['node/add/envelopedetails'] = array(
'title' => 'Envelope Details - Add',
'page callback' => 'envelope_details_add',
'access arguments' => array('access airflight theme'),
'type' => MENU_CALLBACK,
);In my function for that page, I am calling a them template.
function envelope_details_add(){
return theme('envelopedetails_add');
}In my theme, I am trying to render Date Popup but its not working. PLEASE NOTE: Date, Date API and Date Popup modules have been enabled. Date popups are working if I go on a normal, say, Create Content page of a Content Type. Date popup works there but not on my custom page. It leads me to believe that I might need to load some extra module or call a function to bring in required files for it to work.
$format = 'd-m-Y';
$form['datetest'] = array(
'#type' => 'date_popup',
'#title' => 'Select a Date',
'#default_value' => date('Y-m-d'),
'#date_format' => 'Y-m-d',
'#date_label_position' => 'within',
'#date_increment' => 15,
'#date_year_range' => '0:+2',
);
echo drupal_render($form);Can someone please help? I have searched all forums and Google for last 3 hours with no luck. Please help.
Comments
Form elements only work if
Form elements only work if you call drupal_get_form().
You'll want to change your menu hook to use this:
<?php$items['node/add/envelopedetails'] = array(
'title' => 'Envelope Details - Add',
'page callback' => 'drupal_get_form',
'page arguments' => array('envelope_details_add'),
'access arguments' => array('access airflight theme'),
'type' => MENU_CALLBACK,
);
?>
And in doing so, you'll need to turn your callback into a form callback with your form element:
<?php
function envelope_details_add($form, &$form_state){
$format = 'd-m-Y';
$form['datetest'] = array(
'#type' => 'date_popup',
'#title' => 'Select a Date',
'#default_value' => date('Y-m-d'),
'#date_format' => 'Y-m-d',
'#date_label_position' => 'within',
'#date_increment' => 15,
'#date_year_range' => '0:+2',
);
return $form;
}
?>
You'll then need to look at how to theme forms if you want to format this any further.
Jaypan We build websites
Got it
Thanks. I'll try it now. And yes, my next question was going to be about theme and I think I might be able to use $form['#theme'] to be able to do that.
Thanks for quick help.
Working but..
Thanks. Its working like you said.
But Now form_submit hook is not working when I submit the form. I set it as below
function envelope_details_add(&$form_state){$form['#submit'][] = 'envelope_details_add_form_submit';
//More Code, date popup etc
Then I created form_submit to handle that
function envelope_details_add_form_submit(&$form, &$form_state){
print_r($form_state);
exit;
}
But it never gets to that code. I am not sure what I am doing wrong.
EDIT: When I add validate hook, that is working. Its just the submit hook that's not working
$form['#validate'][] = 'envelope_details_add_form_validate';You've named your submit
You've named your submit function incorrectly.
Jaypan We build websites
Can you please clarify? Its
Can you please clarify? Its the same name in #submit and function (envelope_details_add_form_submit). Do I need to something at the end or front?
BTW, I checked
BTW, I checked http://drupal.org/node/222158
Its using same function name in #submit and creating same function to call.
SOLVED!
:sigh: its one of those days. I made a small mistake. Rather than adding submit button in $form, I manually added it in template (). So, Form Builder din't recognize that and kept thinking that form was not submitted.
A small mistake and 3 hours gone.