Community & Support

howto pass default values to new node form?

Hi,

What do I need to do to open a node/add form with default values in it?

Add something to the link I suppose like:
$output .= l("add new custom node", "node/add/custom_node ... ")

And some code in the custom_node_form hook to catch the values I guess.

But how can this be done?

T.i.a.
Harmen

Comments

hidden form fields?

grumble grumble. I wrote an example which got vaporized because of 'suspicious data'

Anyway - try hidden fields and a submit button.
The 'op' should probably be 'preview' but experiment.

You can also 'submit' from a normal link using javascript but I'm not allowed to demonstrate how ;-)

.dan.

thanks dman

I'm looking for this also. Maybe you could post it somewhere else? I'm also trying to figure out how to select taxonomy based on the node/add url. This is default for forum nodes, but for some reason isn't for regular nodes.

This seems to work

<form method='post' action='?q=node/add/story' name='hidden_form'>

<input type='hidden' name='q' value='node/add/story' />
<input type='submit' name='op' value='Preview' />
<input type='hidden' name='op' value='Preview' />
<input type='hidden' name='edit[title]' value='pre-set title' />
<input type='hidden' name='edit[body]' value='pre-set body' />
<input type="hidden" name="edit[type]" value="story" />
<input type='hidden' name="edit[taxonomy][]" value='3' />
</form>

<a href='java_script:document.forms["hidden_form"].submit()'>New page</a>

Seen in action over here

does this still apply?

this doesnt work for me in 4.7.2.. am assuming because it could be classified at an XSS vulnerability?

is there a better way at this point? I need to send a var from a form on one page as a default value for edit['body'] on a node add form page.

i'm stuck.

I guess not

The form API in 4.7 is a lot more secure than that.
This is not exactly an XSS issue, but it is sending data from an untrusted direction, and this needs to be worked around.

You can try to investigate the actual form fields now used to make a node (there will be a hidden form_id field at least that's now needed) and see if filling in a few more blanks helps.

Otherwise, see the examples using CURL to create nodes. Although you don't need the whole network stuff done in that writeup, it should illustrate that the job can still be done remotely. So there's hope yet.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Submit content/ under 5.x/ etc

Seems to have changed a bit under Drupal 5.x, trying to find the fields right now.

Generally, I prefer to accomplish mods like this via a PHP include.

kwt

Works under 5.x...

...if you isolate the forms and do some editing, then feed them back in through a PHP submission to a page node (as suggested above), (ideally by an include IMHO).

Caveats:

Approaching in this manner has some obvious liabilities-- in the case of taxonomy/vocabularies, you're going to wind up hard coding both the tax. #s and the #s of each term-- which could be subject to change.

This might be avoided by a table lookup of the appropriate IDs-- (is there an API or calls for this?...)

FOLLOW-UP: Just look at taxonomy.module for how to parse through $terms for their IDs, and match...

You will also need to customize the users' edit form/hooks or they'll be able to change the options you are pre-setting there, which would be inappropriate, for instance, if you are setting a vocabulary tag and dont' want them to change.

kwt

Sample code?

Some sample php would be great as examples make it much easier for slow folks like me to get what is going on. Thanks.

Use the arg() function to get the default values.

In the simple case you could use node/add/custom_node/arg1_value/arg2_value. This will work in most cases. Since arg(0) is 'node', arg(1) is 'add' the first default value would be arg(3).
Code something like this could be added to you form hook to use the default values

<?php
// Make sure we are adding a node
if ( arg(1) == 'add' ) {
 
// Set the default values using the values passed in
 
$node->field1 = arg(3);
 
$node->field2 = arg(4);
 
// etc
}
?>

Thanks

This works for me although I read somewhere the arg() function should not be used if you can avoid it.

Since it caches the args I see no reason to avoid it.

I have seen people post "altough I read somewhere ..." so many times now it is kind of like an urban legend. Since arg() parses the path once and then caches the results for the page, it is more efficient than parsing the path every time and by using it you can use the standard drupal mechnism for adding new nodes.

Where does this go?

Thanks for these good tips. Where does this code go?

Depends totally

Depends what you want to do with it.

I imagined we wanted a link on a page that would 'make new story in this category' that would open the create content page with the category pre-selected.

The 'issues' submission forms for example do this when you click on 'request feature' - the project/module is already chosen for you.
That however does so in a module-specific way by scraping the path - as discussed using args() above.
My sample is a general-purpose demo pre-filling any field I felt like.
... but where you want to use it is up to you. Probably just paste HTML source in a page..

.dan.

Here's what I did

1. Copied theme_node_form into my theme's template.php (replacing the function name by phptemplate_node_form as usual).
2. Added this line of code to the start of the function:

<?php
$form
['taxonomy'][1]['#value'] = arg(3);
?>

This enabled me to set up URL aliases for each of the different taxonomy terms:

e.g.

submit/news = node/add/story/1
submit/video = node/add/story/2

Then, when going to submit/news the dropdown list for the type of post was pre-set to "news" etc.

Hope this helps!

passing pre-defined title to node/add

I have a block which consists of a form with a single text field and submit button.

<?php

function ask_question() {
 
$form['title'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Ask a question'),
   
'#default_value' => '',
   
'#size' => 50,
   
'#maxlength' => 128,
   
'#required' => FALSE
 
);

 
$form[] = array(
   
'#type' => 'submit',
   
'#value' => t('Post Question'),
  );
 
$form['#method'] = 'get';
 
$form['#action'] = url('node/add/question');

  return
$form;
}

$output = drupal_get_form('ask_question');
return
theme('box', $output);
?>

I need to pre-populate the title form field on the add node page with the passed string. Does anyone have a suggestion of how to accomplish this?

I'd actually prefer to POST the title to the node form page, but this is probably more difficult with Form API.

Thanks in advance.

prepopulate module

The prepopulate module solved my above inquiry nicely by defining the title field #default_value element with the value passed in the URL.

set taxonomy default_value on node form using hook_form_alter

This is for Drupal 6:

If you are developing custom modules, and you have a large site, it might be preferable to set the default_value using hook_form_alter than to add more functions to the template.php file in your theme folder. After a few months working on one site, the template.php file can get unruly. I try to keep as much code as possible in custom modules, rather than in the theme folder.

The following code I used to create a link on a class page that only administrators can see that will automatically select the taxonomy term from the drop down that I want when clicked on. For example "http://www.mysite.com/node/add/custom-node-type/497

You could probably even set this and then hide the taxonomy field. If you had users entering the node, you might want to try to do that. If you have admin's, this approach is fine:

<?php
function myModule_form_alter(&$form, $form_state, $form_id) {
    if(
$form_id == 'custom_node_type_node_form') {

    if(
is_numeric(arg(3))) {

       
// automatically set the default value on the taxonomy terms for that node form
       
       
$form['taxonomy'][4]["#default_value"][0] = arg(3);   
       
       



       
    }
   
    }
}
?>

Nate Andersen
Web Developer
RockstarNinja.net

http://drupal.org/node/214592

As well,

As well, see:

http://drupal.org/node/116541, "HOWTO: Select Drupal Form Elements"