Skip to content
Daniel Schöni edited this page Mar 23, 2022 · 10 revisions

Simple usage

Prepopulate the title field on a node creation form: http://www.example.com/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

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. Backdrop 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 Backdrop form. Once you find the appropriate (or <textarea>…</textarea> tag, use the value of the name attribute in your URL, contained in the edit array. For example, if the 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

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">

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. 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. Author field

http://www.example.com/node/add/content?edit[author][name]=dragondrop

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.

Example: Content of Organic Groups

To use a Books content on Organic Groups, it would be helpful, to automatically fill in the groups association to the Groups audience field (og_group_ref). After Creation of a new book content, which is assigned as group content, the book contains this field with the title "Your groups", which by default contains the entry "- None -".

Viewing the HTML code for the selection, in this case as List, we will see its structure, something like:

<select multiple="multiple" name="og_group_ref[und][0][default][]" id="edit-og-group-ref-und-0-default" class="form-select">
    <option value="_none" selected="selected">- None -</option>
    <option value="7">Group 1</option>
    <option value="9">Group 2</option>
    <option value="10">Group 3</option>
    <option value="15">Group 4</option>
</select> 

So, there is the name to use with the Prepopulate module: name="og_group_ref[und][0][default][]", to use as [og_group_ref][und][0][default].

Creating a View with a Link for "New Content"

A View for User accounts is required. The following views configuration allows it, to be displayed only in a group with valid membership:

RELATIONSHIPS

OG membership: OG membership from User account
(OG membership from user) OG membership: Group Node from OG membership

CONTEXTUAL FILTERS

(OG membership from user) OG membership: Group ID -> set to "Provide default value" and "Content ID from URL"

FILTER CRITERIA

User account: Active (Active)
User account: Current (Yes)

FIELDS

(Group node from OG membership) Content: Title
    Settings "Rewrite results":
    Text: "Create new Book ..."
    Checked: "Output this field as a link"
    Link path: /node/add/book?edit[og_group_ref][und][0][default]=%1

Editing the Title field, further below in the field settings, there is a description of "Replacement patterns", which when opened, reveals which tokens are usable as replacemet terms:

- [title] == Content: Title
- %1 == OG membership: Group ID title
- !1 == OG membership: Group ID input

The entry for the "Link Path:" is as follows, depending of the fields configuration:

  • For Lists: /node/add/book?edit[og_group_ref][und][0][default]=%1
  • For Checkboxes: /node/add/book?edit[og_group_ref][und][0][default][%1]=%1
  • For Autocomplete (Tags style): /node/add/book?edit[og_group_ref][und][0][default]=[title]

Using this view in a block on the sidebar (as example), gives there a link "Create new Book ...", which automatically fills in the groups association of the open group in the main content view to the new content, when used.