Automatically Populate Drupal Fields From Other Content

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:

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

Author

  • Steve Burge

    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.

0 0 votes
Article Rating
Subscribe
Notify of
30 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Steve Polito
Steve Polito
8 years ago

I feel like this will definitely come in handy at some point. I had no idea you could even do this. Just out of curiosity, couldn’t you have bypassed the “Speaker Bio” and “How To Contact The Speaker” fields all together and displayed them by configuring the entity reference field display? Your method is definitely more flexible though, incase the client doesn’t want to use the current data being populated, they can just update the field values.

steve
steve
8 years ago
Reply to  Steve Polito

Hey Steve. Yes, that’s right, you could modify the entity reference display and that would be good in some situations.
One other advantage of the solution in this post: you can mix up the display of the fields so all the pre-populated fields don’t need to be together.

Capi Etheriel
Capi Etheriel
8 years ago
Reply to  steve

Well, you could load the referenced node and go crazy with data display in the theme layer…

sonicthoughts
8 years ago

Wow. That was amazing. Never realized that could work.

joewhitsitt
joewhitsitt
8 years ago

I feel like pulling in a view mode of the referenced entity would be better in most cases so that you aren’t reproducing content across a site, which could lead to fragmentation the moment one node is updated.

Shawn Dryden
Shawn Dryden
8 years ago
Reply to  joewhitsitt

I think the token would grab the updated content. You are reproducing the content either way, token or view mode.

joewhitsitt
joewhitsitt
8 years ago
Reply to  Shawn Dryden

oh gosh, i wasn’t awake enough when i wrote that. some how i glazed right over and thought it was just going to pre-populate the field with default text for the editor to manipulate.

Horia Rudan
Horia Rudan
8 years ago

This is an interesting approach. But what happens when a presentation is held by multiple speakers?

I’m using a similar tehnique on one of my websites, but what I do is:

– The Speaker content type has a term reference field where I just copy the speaker’s name (with the autocomplete widget and allow the creation of new terms. This will populate my “speakers” taxonomy vocabulary while tagging my speaker with its own name.

– The Presentation content type also has a term reference field (same vocabulary), autocomplete widget but cannot create new terms. Tagging a presentation with a term from the Speakers vocabulary (or multiple terms) will result in both nodes (speaker and presentation) being tagged with the same term.

– Create a view (block) that uses a contextual filter to display the speaker node (in whatever view mode) tagged with the same term as the current page (getting the value from URL). Make the view available as a block. All is left to do is add a new filed of type block reference (you need to install the module) to the Presentation content type with the views block as the default value. All speakers referenced in the term reference field will be displayed in the block.

steve
steve
8 years ago
Reply to  Horia Rudan

@horiarudan Wonderful, thanks for such a detailed suggestion!

Harpreet Kaur
Harpreet Kaur
6 years ago
Reply to  Horia Rudan

hi..could you please help me doing the same in drupal 8?

Jake
Jake
3 years ago
Reply to  Harpreet Kaur

Yep, me too… seems like much that worked in Drupal 7 is broken in Drupal 8.  Funny thing is Drupal.org is working on Drupal 9!

Bobby Richmond
Bobby Richmond
8 years ago

Nice steve, I’ve run into a little issue with my pre-populate fields. wondering if you could help a bit?
I have 2 content types a job and an application, inside jobs is a link to application and inside application in an entity reference to the job title.
I want when people select to “apply” on the job, it will allow them to create an application (already have this part done), however the job name field should be pre-populated.
I have it all the way to the point that if you manually enter the job id in the url it will pre-populate. I’ve tried many tokens, but the url doesnt seem to take the value of them. It leaves them as [node:field-job:nid]
Great tutorial above and if you can help It would be appreciated thanks.

steve
steve
8 years ago
Reply to  Bobby Richmond

Bobby Richmond Hi Bobby, if you need detailed support on your situation, I’d recommend becoming a member: [url=https://www.ostraining.com/drupal-support/]https://www.ostraining.com/…[/url]
My guess is that you’ve chosen the wrong token from the Help > Token screen.

Bobby Richmond
Bobby Richmond
8 years ago
Reply to  steve

Hi Steve, after days of looking for a solution I found out the issue(I believe)
The problem was I was using the token(s) in a text field, which apparently isnt supported fully. Luckily I found a module that allowed for such things its called the advanced text formatter ([url=https://www.drupal.org/project/advanced_text_formatter]https://www.drupal.org/proj…[/url] ), it allowed me to add tokens to it. Once that was available to me I used the [node:nid] token and everything came out perfectly thanks for the timely reply!

steve
steve
8 years ago
Reply to  Bobby Richmond

@bobby_richmond Yes, that’s right. Perhaps we should emphasise that point in the tutorial, but it’s there at the beginning …
“Before you begin, follow these steps to enable tokens in Drupal fields”

Lewis
Lewis
7 years ago

Thanks. This works well for putting tokens into text fields, but I am wondering about, for example, using the date field from a conference to populate the date field for a presentation from that conference.
It seems clunky to have to display the date as a text field, when it already has its own formatting.
Thoughts?

Nick
7 years ago
Reply to  Lewis

Great question, Lewis! If you sign up for one of our support plans at [url=https://www.ostraining.com/pricing/]https://www.ostraining.com/…[/url] , one of our Drupal techs will be able to help.

Lewis
Lewis
7 years ago
Reply to  Nick Savov

Hi Nick. Sorry. I have a support account and thought I was logged in when I posted. I think I get it now. These discussions are not for ‘support’ people and we should ask our questions from the inside, as it were. No worries.
Cheers,

Lewis

Nick
7 years ago
Reply to  Lewis

Thanks Lewis! That’s right. The blog comments are only loosely monitored and our support area gets our top priority.

Mr TK
Mr TK
7 years ago

This is very helpful. I am new to Drupal and PHP and I was wondering how I can get the value of an entity reference in a computed field. This has given the solution.

steve
steve
7 years ago
Reply to  Mr TK

Great, glad to hear it helped

Harpreet Kaur
Harpreet Kaur
6 years ago

i tried the above steps but the speaker bio and how to contact speaker fields are not retrieving the data from speaker content instead of that the result is showing the default entered value, that is [node:field_speaker_link:field_how_to_contact]

Harpreet Kaur
Harpreet Kaur
6 years ago

[urlcomment image]https://uploads.disquscdn.c…[/url]
i tried the above steps but presentation content is not retrieving the data from speaker content instead of that its just showing the token value that are entered in default value field.

sam
sam
6 years ago
Reply to  Harpreet Kaur

I believe it’s because you didnt fully configured your HTML filter to let tokens pass trough

[url=https://www.ostraining.com/blog/drupal/tokens-in-drupal-fields/]https://www.ostraining.com/…[/url] (config > admin html filter edit > enable tokens

egarbeil
6 years ago

I noticed this only works after the node is saved. How do you get the data into the field so that it is available to other parts of the form ( I need to do calculations with this field) before saving?

Ignis
Ignis
6 years ago

Hello Steve, Awesome write up you hot here. I tried it but it seems now to work for decimal fields.
I have a Rates content type with Rate category and Amount fields, Then I have a Ticket content type with Rate Category (reference from Rates)and Amount (To be populated on Selected rate category). but its not working. any help with that?

sushant bhargav
sushant bhargav
6 years ago

Suppose I want to add, multiply, concate or copy the field data at the time of input, not after that, how can I do that. If using jquery is your suggestion, please tell me some way that I can do frequently rather than usual way.

egarbeil
6 years ago

Ignis & sushant bhargav – 2 modules that I found to do the job 1) entityform autofill, which will pre-populate fields based on a reference field – It works after the reference field is filled in, while you are in the form. and 2) math module – which allows you to do calculations based on the field. computed field module is also good – but that’s straight PHP and I found the math module adequate. The thing I’m struggling with now is that I need to make the fields display only so that the user can’t change the calculated and pre-populated fields. All work while you are in the form.

sushant bhargav
sushant bhargav
6 years ago

Thanks Elisabeth, thanks for replying, I should have mentioned that I am using drupal 8, I am looking into it though.
On reading the module description , it seems it doesn’t works on re-writing the field, since you already tried it , is that true ?

egarbeil
6 years ago

The module in the original post doesn’t work while you are in the form, entity form autofill does and it works really well. The math and compute modules do too. I’m using all of them at the moment. I think all three have a Drupal 8 release but you would need to check.

30
0
Would love your thoughts, please comment.x
()
x