Prepopulate is an attempt to solve the problem that resulted from the discussion where the $node object, it was (correctly, I believe) decided, should not be prefilled from the $_GET variables. Instead, the power of the FormsAPI should be used to modify the #default_value of the form elements themselves.
NOTE: see https://www.drupal.org/node/883980 for updated information about the 2.x branch of prepopulate.
This functionality will make things like bookmarklets easier to write, since it allows forms to be prefilled from the URL, using a syntax like:
http://www.example.com/node/add/blog?edit[title]=this is the title&edit[body]=body goes here
Please report any bugs or feature requests to the Prepopulate issue queue.
Instructions for use
Simple usage
Prepopulate the title field on a node creation form:
http://www.example.com/node/add/content?edit[title]=This is the title
With 'non-clean' urls:
http://www.example.com?q=node/add/content&edit[title]=This is the title
Multiple fields
Prepopulate can handle pre-filling multiple fields from one URL. Just separate the edit variables with an ampersand:
http://www.example.com/node/add/content?edit[title]=The title&edit[body_filter][body]=The body
You're already using the ampersand with non-clean URLs:
http://www.example.com?q=node/add/content&edit[title]=The title&edit[body_filter][body]=The body
How to find what variable to set
This can be tricky, but there are a few things to keep in mind that should help.
Prepopulate.module is quite simple. It looks through the form, looking for a variable that matches the name given on the URL, and puts the value in when it finds a match. Drupal keeps HTML form entities in an edit[] array structure. All your variables will be contained within the edit[] array.
A good starting point is to look at the HTML code of a rendered Drupal form. Once you find the appropriate <input /> (or <textarea>…</textarea> tag, use the value of the name attribute in your URL, contained in the
edit array. For example, if the <input /> tag looks like this:
<input id="edit-title" class="form-text required" type="text" value="" size="60" name="title" maxlength="128"/>
then try this URL:
http://www.example.com/node/add/content?edit[title]=Automatic filled in title
Taxonomy
In this example, [taxonomy][1] is the vocabulary ID and the number 2 is the Term ID that will populate the field:
http://www.example.com/node/add/content?edit[taxonomy][1]=2
If this doesn't work, you may want to try this format for a vocabulary named "tags":
http://www.example.com/node/add/content?edit[taxonomy][tags][1]=test
For 8.x-2.x:
Taxonomy fields with widget type select list and term ID:
http://www.example.com/node/add/node?edit[field_name]=92
<select name="field_name" id="edit-field-name">
<option value="92">Term 1</option>
<option value="93">Term 2</option>
For 7.x-2.0:
Taxonomy fields with widget type taxonomy autocomplete and termname:
http://www.example.com/node/add/node?edit[field_ch_projekt][und]=termname
<input type="text" name="field_ch_projekt_3[und]" id="edit-field-ch-projekt-3-und">
Taxonomy fields with widget type select list and term ID:
http://www.example.com/node/add/node?edit[field_ch_projekt_2][und]=92
<select name="field_ch_projekt_2[und]" id="edit-field-ch-projekt-2-und">
Taxonomy fields with radios and term ID:
http://www.example.com/node/add/node?edit[field_ch_projekt][und][92]=92
<input type="radio" value="92" name="field_ch_projekt[und]" id="edit-field-ch-projekt-und-92">
Taxonomy fields with checkbox and term name or term ID
http://www.example.com/node/add/node?edit[field_ch_projekt][und][92]=92
http://www.example.com/node/add/node?edit[field_ch_projekt][und][92]=termname
with multiple term IDs
http://www.example.com/node/add/node??edit[field_ch_projekt][und][92]=92&edit[field_ch_projekt][und][93]=93
<input type="checkbox" checked="checked" value="92" name="field_ch_projekt[und][92]" id="edit-field-ch-projekt-und-92">
<input type="checkbox" checked="checked" value="93" name="field_ch_projekt[und][93]" id="edit-field-ch-projekt-und-93">
To find the vocabulary ID and the number:
- Install https://www.drupal.org/project/bundle_copy
- Then go to Structure → Taxonomy → Export → "Your Term", then you can see your term VID, i.e.
'vid' => '5',
CCK
CCK fields are a bit more complicated:
<input id="edit-field-office-0-node-name" class="form-text form-autocomplete" type="text" value="" size="60" name="field_office[0][node_name]" maxlength="128" autocomplete="OFF"/>
The key is to put this in the edit[] array nested, like this:
http://www.example.com/node/add/content?edit[field_office][0][node_name]=AL-235
Another example:
<textarea id="edit-field-content-0-value" class="form-textarea resizable processed" name="field_content[0][value]" rows="10"
cols="60"/>
would be:
http://www.example.com/node/add/content?edit[field_content][0][value]=A long text string
and, again, for non-clean URLs, it's:
http://www.example.com?q=node/add/content&edit[field_content][0][value]=A long text string
If you are using Field Groups you will need to add the field group id to the URL as well. Using the same example as above you would use:
http://www.example.com/node/add/content?edit[group_id][field_content][0][value]=A long text string
and, again, for non-clean URLs, it's:
http://www.example.com?q=node/add/content&edit[group_id][field_content][0][value]=A long text string
Special cases
Body fields
Body fields are different. Though their HTML entity looks like this:
<textarea id="edit-body" class="form-textarea resizable processed" name="body" rows="20" cols="60"/>
You can't just take the name "body," throw it into a edit[body] and expect it to work. Drupal wraps the body field into a "body_filter" array when it gets processed. So, for body fields in Drupal 5, a URL like:
http://www.example.com/node/add/content?edit[body_filter][body]=This is the body
ought to do the trick. And for Drupal 6 this URL should work:
http://www.example.com/node/add/content?edit[body_field][body]=This is the body
For Drupal 7 this URL should work:
http://www.example.com/node/add/content?edit[body][und][0][value]=this is the body
Entity references
Entity reference fields (created with the entityreference module) using the autocomplete widget need to be prepopulated by setting both the label followed by the entity ID in parentheses:
http://example.org/node/add/content?edit[field_example_entity][und][DELTA][target_id]=Entity%20label%20(ENTITY_ID)
For entity reference fields using radios, checkboxes, or select elements, see the taxonomy section above.
Entity references Drupal 8
Pre-populate an entity reference field:
http://www.example.com/node/add/page?edit[field_entity_reference][widget][0][target_id]=123
Entity references where reference method is view (Drupal 8)
http://example.org/node/add/content?edit[field_example_entity][widget]=123
Dynamic entity references (DER-field) Drupal 8:
Prepopulate DER with (Form Widget) Autocomplete
Autocomplete is currently not working with the current version of prepopulate or der.
Prepopulate DER with (Form Widget) Select list
Specify the content type + "-" + id:
For a node:
http://example.org/node/add/content?edit[field_der_example_entity][widget]=node-123
For a term:
http://example.org/node/add/content?edit[field_der_example_entity][widget]=taxonomy_term-123
Author field
http://www.example.com/node/add/content?edit[author][name]=dries
Status field
To prepopulate the node status as unpublished:
http://www.example.com/node/add/content?edit[options][status]=0
Escaping special characters
Some characters can't be put into URLs. Spaces, for example, work mostly, but occasionally they'll have to be replaced with the string %20. This is known as "percent encoding." Wikipedia has a partial list of percent codes at:
https://en.wikipedia.org/wiki/Percent-encoding
If you're having trouble getting content into field names, or are getting 'page not found' errors from Drupal, you should check to ensure that illegal characters are properly encoded.