Not sure where to post help but some how landed here.
So here is my set up,
I have a content type - form - which I have pasted php into the body field and which is based on the form API. See code below...
Then I have a content type - competition - which has a title, body and node reference fields. The node reference field is set tp Full Node and is configured to load a form content node.
What I would like to do is take the competition node title and pass it to the form node and place it in a field so when the form is submitted the title of the page it was submitted from is submitted too.
So far I haven't been successful and I can't find too much information about passing $node variables into form functions. You can see I am trying to call the $node->title in the form but this returns nothing.
Any help is appreciated.
<?php
// This function is called the "form builder". It builds the form.
// Notice, it takes one argument, the $form_state
function my_module_my_form($form_state) {
$form['Form'] = array(
'#type' => 'textfield',
'#title' => t('Form'),
'#default_value' => $node->title,
'#required' => TRUE,
);
$form['Firstname'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#required' => TRUE,
);
$form['Surname'] = array(
'#type' => 'textfield',
'#title' => t('Surname'),
'#required' => TRUE,
);
$form['Email'] = array(
'#type' => 'textfield',
'#title' => t('Email Address'),
'#required' => TRUE,
);
$form['Birthdate'] = array(
'#type' => 'date',
'#title' => t('Birthdate'),
'#default_value' => array('day' => '', 'month' => '','year' => ''),
'#required' => true,
'#after_build' => array('my_date_callback'),
'#element_validate' => array('my_module_my_form_validate'),
'#required' => TRUE,
);
$form['Phone'] = array(
'#type' => 'textfield',
'#title' => t('Contact Number'),
'#required' => TRUE,
);
$form['Address1'] = array(
'#type' => 'textfield',
'#title' => t('Address 1'),
'#required' => TRUE,
);
$form['Address2'] = array(
'#type' => 'textfield',
'#title' => t('Address 2'),
);
$form['Suburb'] = array(
'#type' => 'textfield',
'#title' => t('Suburb'),
'#required' => TRUE,
);
$form['State'] = array(
'#type' => 'select',
'#title' => t('State'),
'#required' => TRUE,
'#options' => array(
'--',
'VIC',
'NSW',
'SA',
'WA',
'QLD',
'TAS',
'ACT',
),
'#required' => TRUE,
);
$form['Postcode'] = array(
'#type' => 'textfield',
'#title' => t('Post Code'),
'#required' => TRUE,
);
$form['Entry'] = array(
'#type' => 'textarea',
'#title' => t('Entry'),
'#required' => TRUE,
);
$form['Misc']['Promotions'] = array(
'#type' => 'checkbox',
'#title' => t('Yes, I would like to receive offers and promotions.'),
);
$form['Misc']['Terms'] = array(
'#type' => 'checkbox',
'#title' => t('Yes, I have read and accepted the Terms and Conditions of this competition.'),
'#required' => TRUE,
);
// Make sure we have a submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Enter')
);
return $form;
}
function my_date_callback($form_element, $form_values){
$aTmp = array('' => '--');
$form_element['day']['#options'] = $aTmp + drupal_map_assoc(range(1, 31));
$form_element['month']['#options'] = $aTmp + drupal_map_assoc(range(1, 12), 'map_month');
$form_element['year']['#options'] = $aTmp + drupal_map_assoc(range(1900, 2050));
return $form_element;
}
function my_module_my_form_validate($form, &$form_state) {
if (!$form_state['values']['Firstname']) {
form_set_error('Firstname', 'Please enter your first name.');
}
if (!$form_state['values']['Surname']) {
form_set_error('Surname', 'Please enter your surname.');
}
if (!$form_state['values']['Email']) {
form_set_error('Email', 'Please enter your email address.');
}
if (!$form_state['values']['Birthdate']['day'] || !$form_state['values']['Birthdate']['month'] || !$form_state['values']['Birthdate']['year']) {
form_set_error('Birthdate', 'Please select your birthdate.');
}
if (!$form_state['values']['Phone']) {
form_set_error('Phone', 'Please enter your contact number.');
}
if (!$form_state['values']['Address1']) {
form_set_error('Address1', 'Please enter your address.');
}
if (!$form_state['values']['Suburb']) {
form_set_error('Suburb', 'Please enter your suburb.');
}
if (!$form_state['values']['State']) {
form_set_error('State', 'Please select your state.');
}
if (!$form_state['values']['Postcode']) {
form_set_error('Postcode', 'Please enter your post code.');
}
if (!$form_state['values']['Entry']) {
form_set_error('Entry', 'Please fill in your entry.');
}
if (!$form_state['values']['Terms']) {
form_set_error('Terms', 'You must agree to the terms and conditions.');
}
}
function my_module_my_form_submit($form, &$form_state) {
drupal_set_message(t('Thank you, your entry has been submitted.'));
}
return drupal_get_form('my_module_my_form');
?>
Comments
Hi hj, I'm a bit of a noob,
Hi hj,
I'm a bit of a noob, so bear with me. I'm sure others will post later on, but have you tried pasting your functions into a custom module, not template?
Yes I began with a custom
Yes I began with a custom module then moved to a template and now I've moved to the body of a page. I just updated the post, sorry. Thanks for your reply tho.
Right, so the following code
Right, so the following code will return the title of the node which is parent to the referenced node.
$node_path = explode("/",$_GET['q']);$node_id = $node_path[1];
$query = db_query("SELECT title FROM {node} WHERE nid = ".$node_id);
$result = db_fetch_array($query);
$title = $result["title"];
print t("Form Title: ".$title."");
The issue now is trying to get the result into the form field.
I have come across a new error since the first post which relates to putting functions in the body of a page which is being referenced.
If you reference a full node on node "competition" which contains node "form" which contains the form API functions and both the form node and competition node are displayed on the front page for example you get
Perhaps an easy fix would be to stop the form from being promoted to the front page but I'd much rather fix the problem so that it didn't matter if both the node containing the form and the node which is the form could be displayed on the same page.
Another Approach: Client-Side with jQuery
So working with the code you've already published, you can replace your last 'print' line to print out a hidden input element, and then add in a line to call a .js include, like so:
$title = $result['title'];print "<input type='hidden' id='my_title' value='$title'>";
// Include a JS file which will reference the hidden input to load that data into the text field.
drupal_add_js('/path/to/js/file/title_loader.js');
Then, in your title_loader.js file, just put something like this:
// Run after the page is done loadingjQuery(document).ready(function() {
// Get our variable
var title_text = $('#my_title').val();
alert('Node title='+title_text);
// Load it into the text input, assuming it has id='input_target'
$('#input_target').val(title_text);
alert('Done! Text input populated with title value.');
});
BTW, this assumes you are using jQuery.
Passing the Node from the tpl file to the form function
I did this recently with the following steps;
In the node.tpl.php (or content type tpl) file, reference the form function and pass the $node as an additional argument.
$myform = drupal_get_form("foo_standard_form", $node);
Add the $node argument to the function that builds your form.
example: function foo_standard_form($form_state, &$node) { ... }
Add the $node->title to the hidden input.
$form['foo_node_title'] = array(
'#type' => 'hidden',
'#default_value' => ($node->title) ? $node->title: "My Node Title Default",
'#weight' => -100,
);
In a nutshell this allows the tpl file to pass an extra parameters, i.e. the current node, to the form. Once the form function has the node object you have full access to node attributes. In thing to node, the function gets the form state added by default at the first argument by the menu call back. If you get your arguments out of alignment, then the node will contain the wrong object. And drupal should give you an arguments error.
Hope this helps.
Stephan
Long Time ASP web developer, who has seen the light and is 100% committed to the drupal way of development.
Well, i think that this is by
Well, i think that this is by far the most clear way to do it, thx bobodeman.
We can also use a cookie or a
We can also use a cookie or a session but this solution is good too and helpful.
Thanks
Bilal Hakim
Imprimante | Pilote imprimante
Forms and Nodes
<?php
function yourmodule_form($node) {
$form[] ... blah blah...
return $form;
}
?>
you need to get that $node into that function before you can use it:
@dkinzer - how do you get a
@dkinzer - how do you get a handle to the node so you can pass it into the function? I have looked through all the comments here and quite a few other sites that came up in my searching but so far it is not working for me. THanks in advance.
sedu hair straighteners | chi flat irons