Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By carlovdb on
Hello,
For my site, I have to create a custom form in a custom template. It is not in a custom block, but in a custom tpl-file. I have searched the internet for a solution, but or the form is not shown or the fields are shown, but not in a < form > tag. This way, the submit-button does not work. When I render the fields in a self-written form-tag, the validate function and submit-function don't work.
Can someone help me?
TPL-File
$form = _odisee_forms($form, $dag, $datum, $uur, $campussen);
$form['infodag'] = form_process_checkboxes($form['infodag']);
$formulier = drupal_get_form('infodagen-block-form');
print '<form id="'.$formulier['#id'].'" accept-charset="UTF-8" method="'.$formulier['#method'].'" action="'.$formulier['#action'].'">';
print drupal_render_children($form);
//print render($form);
print render($formulier['form_id']);
print render($formulier['form_build_id']);
print render($formulier['form_token']);
print '</form>';
forms.inc
function _odisee_forms($type, $dag, $datum, $uur, $campussen) {
$form = array();
foreach ($campussen as $key => $campus) {
$infomoment[] = t($dag[$key])." ".$datum[$key]. " @ " .$campus. " om ".$uur[$key];
}
$form['#id'] = 'infodagen-block-form';
$form['infodag'] = array(
'#type' => 'checkboxes',
'#options' => $infomoment,
'#required' => TRUE,
);
$form['voornaam'] = array(
'#type' => 'textfield',
'#title' => t('<img src="/sites/all/modules/custom/odisee_forms/images/icon.png" class="icon">'),
'#size' => 15,
'#attributes' => array(
'placeholder' => array('Voornaam'),
'class' => array('breed')
),
'#required' => TRUE,
);
$form['naam'] = array(
'#type' => 'textfield',
'#title' => t('<img src="/sites/all/modules/custom/odisee_forms/images/icon.png" class="icon">'),
'#size' => 15,
'#attributes' => array(
'placeholder' => array('Naam'),
'class' => array('breed')
),
'#required' => TRUE,
);
$form['emailadres'] = array(
'#type' => 'textfield',
'#title' => t('<img src="/sites/all/modules/custom/odisee_forms/images/arroba-symbol.png" class="icon">'),
'#size' => 20,
'#maxlength' => 128,
'#required' => TRUE,
'#attributes' => array(
'placeholder' => array('E-mailadres'),
'class' => array('breed')
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Schrijf in!'),
'#attributes' => array('class' => array('button')),
'#submit' => array('_odisee_form_submit'),
'#validate' => array('_odisee_form_validate'),
);
return $form;
}
function _odisee_forms_validate($form_id, &$form, &$form_state) {
dpm($form_state, "state");
dpm($form);
}
function _odisee_forms_submit($form, &$form_state) {
}
Comments
I think this page might
I think this page might answer your question:
http://drupal.stackexchange.com/questions/312/how-to-a-use-a-template-fi...
You need to create a
You need to create a preprocess function for your form.
Now in your template file you can print your form:
Hello,
Hello,
Thank you for your answer.
So in my case, which themekey do I use? Is it THEMENAME_preprocess_forms(&$vars) ?
You said you're using a
You said you're using a custom template. Please show the hook_theme() implementation you are using to register that template. That will help me tell you the THEMEKEY
Yes, I have my own custom
Yes, I have my own custom template(theme), but no hook_theme
How are you adding the
How are you adding the template file to the site then?
Sorry, I meant no specific
Sorry, I meant no specific hook_theme for the form.
Sorry, I meant no specific
Ok, so how are you adding the template to the site? How is it being included? And what is the template name?
I use field_collections in my
I use field_collections in my site, maybe it is important to mention.
The template where I want to show the form is: odisee-block-vierkant-2-2.tpl.php
It is included in the field-collection-item--field-dynamic-blocks.tpl.php
Ok, then go back to my
Ok, then go back to my original post about the preprocess function, and you need to use:
Where THEMEKEY is the machine name of your theme.
When I do that, the form is
When I do that, the form is created, but the input fields are not shown. That was my original problem. :(
Than I get this as HTML
At the start of your form
At the start of your form definition, you need to call form_load_include(), since your form is not in the .module file.
Am I doing something wrong?
Am I doing something wrong? Because that has no result..
At the end I have form_load_include($form_state, 'inc', 'odisee_forms');
Assuming your module is named
Assuming your module is named odisee_forms, and that your include file is named forms.inc, you need this:
I am really sorry. But I
I am really sorry. But I still get an empty form :(
Please show all your current
Please show all your current code.
In sites/all/themes/odisee
In sites/all/themes/odisee/templates I have: field-collection-item--field-dynamic-blocks.tpl.php
?>
Based on which field collection type they choose I print a new template
In odisee.module I have an odisee_theme
The form should only be shown in odisee_block_vierkant_2_2
elseif ($type_row == 'form'):
print $messages;
if($form == 'Infodagen'):
endif;
endif;
For the form, I have these files
odisee_forms.module
functions.inc
forms.inc
I see three major problems.
I see three major problems.
1)
drupal_get_form('odisee-forms');
Your form is called odisee_forms() not odisee-forms(). You are calling the incorrect form name.
2)
function odisee_forms($type, $dag, $datum, $uur, $campussen)
Your form definition does not declare $form and &$form_state as the first arguments. All form definitions require these two arguments.
3)
form_load_include($form_state, 'inc', 'odisee_forms', 'forms');
Looking at the rest of your code, it appears you should be using
includes/forms
as the last argument.Oh my god. Three stupid
Oh my god. Three stupid errors. (ashamed)
I was changing my code so much that I overlooked these errors.
Thank you for your major help!
Sorry for this new question
Sorry for this new question Jaypan.
Now I have a strange thing going on with the submit- and validation handler.
When I don't fill in the fields and I click on the submit-button. The page reloads, but the message of the fields being required is not shown. When I hit the submit-button again, the message of required fields is shown.
But it seems that it is one step behind.
For example:
- No field is filled in and I hit the submit button, nothing happens.
- I fill in 1 field and I hit the submit button, the message of required fields comes for the tree fields. This is incorrect, because I have 1 field filled in.
- I hit the submit button, the message of 2 fieds are not filled is is correct.
Nothing further happened to the code, just some css issues.
Are you defining the form
Are you defining the form inside the template file itself?
I define my form in forms.inc
I define my form in forms.inc.
in odisee-block-vierkant-2-2.tpl.php I print the form
print $my_form;
in forms.inc, I also create the form
I don't really have an answer
I don't really have an answer. Try putting everything into the .module file.
But I do notice that the
But I do notice that the order of your arguments in your form definition is incorrect.
Do you mean the creation of
Do you mean the creation of my function?
function odisee_forms($type, $dag, $datum, $uur, $campussen, $form, &$form_state) {
Yes. $form and &$form_state
Yes. $form and &$form_state should be the first two arguments in form definitions.
That did not do the tric..
That did not do the tric.. too bad
Hello,
Hello,
I restarted everything and cleaned up my code.
To make the form work, I printed this code in my .tpl.php-file
print drupal_render(drupal_get_form('odisee_forms', $dag, $datum, $uur, $campussen));
Now, the form can be submitted and this code is also executed. (the data is saved in the database).
There is 1 problem. Can you help me?
The error messages (status_messages) from the validation-function are not shown. Even if I use the standard validation-function. (The only thing that needed to be checked is the field is filled in. #required)
If I try to do it with ajax, nothing happens. This is what I added in forms.inc
I also tried this
Try changing this:
Try changing this:
To this:
Hello,
Hello,
That doesn't change a thing. It looks like the form does not reload. The site does not display the messages also.
kind regards.
To be sure, the callback is
To be sure, the callback is being called. I tested it with a javascript alert.
Forms.inc - callback function
javascript-file
Try this:
Try this:
No :(
No :(
Not succesful.. Like It looks like the site doesn't show the messages
What happens if you add a
What happens if you add a message right above that:
If it doesn't show, then the problem isn't in your code that you're showing, it's somewhere else in your system.
Yes, it shows the messages.
Yes, it shows the messages.
Because of your tip to show in the body, I changed the ajax_command_prepend to the div of the form and it works:
$commands[] = ajax_command_prepend('form#odisee-forms--3', theme('status_messages'));
Jaypan Your are awesome
Jaypan - I have been building a custom module to pull in some api data, no specific question, just a compliment on your thorough answers and patients with those asking questions. I have found that you have answered questions that I needed answers a number of times. So Thanks.