Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Automatically Populate Drupal Fields From Other Content

Parent Feed: 
prepopulate

One of our members wanted to automatically fill in Drupal fields from another node.

It took a little experimentation, but we managed to make it work using the Token, Token Filter and Entity Reference modules.

This technique works for nodes, but it should allow you to automatically popular field using data from users, comment or any other entity.

Before you begin, follow these steps to enable tokens in Drupal fields. You must allow tokens inside fields before this tutorial will work.

In this example, we have two content types: Presentations and Speakers.

Here's how it will work: when people add a Presentation, they choose a speaker and have the speaker details automatically fill into the Presentation field.

Presentation and Speakers Content type

Here's our Speakers content type with the information fields:

Drupal speakers content type with fields
  • Here's how the speaker content type appears after publication:
Automatically Populate Drupal Fields From Other Content

Here's our Presentations content type. Note that we have an "Entity Reference" link to the speaker. This field is the key to the whole process.

  • If you don't have Entity Reference, install it and make a field that links to the Speaker content type.

Now that we have out content types set up, let's use the Token module to link their data together.

  • Go to Help > Token and you'll see a list of all the tokens available.
  • Look down the list to find your Entity Reference field. Click "Nodes", then your field name.

Underneath your Entity Reference field, you'll see the tokens from your Speaker content type. These tokens contain the information that we want to pass on to Presentations.

  • Go to Structure > Content type > Presentations.
  • Put your tokens inside the "Default Value" for the field. Below we have the Speaker Bio field:
the Speaker Bio field in Drupal
  • Here we have the "How to Contact the Speaker" field:
How to Contact the Speaker field
  • When you've saved the fields above, go to "Add content" and click "Presentation".
  • The image below shows how the Presentation screen appears. The "Speaker Link" field is controlling where the information is being taken from.
  • Save the Presentation.
  • Your data will be automatically populated:
Automatically populated content in Drupal

About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web