In my content type there is a text field that is set using a list (combobox).

When adding new content, the list is displayed with static elements.

I would like to query an external site when the user adds content (or edits the page) and then build the list from the query result. The user can then select from these elements.

Is that possible?

  • This is for Drupal 8 or 7? – Adrian Cid Almaguer Nov 8 '18 at 18:13
  • @AdrianCidAlmaguer sorry I missed that. I am currently using Drupal 7 but a solution for Drupal 8 is also interesting because I plan to migrate in the coming month. – testalino Nov 9 '18 at 7:43
  • Well is possible for Drupal 7. You only need to search now. The concept is the same. – Adrian Cid Almaguer Nov 9 '18 at 14:07

For Drupal 8

Yes, is possible, but you will need to implement your own field type.

You should take a look into the Country module as you will need to implement the OptionsProviderInterface interface.

In the getPossibleOptions method, you will query the external site and get the possible options for your field.

For Drupal 7

You can always take a look inside the Examples module even for Drupal 8 you will find a very useful boilerplate code for your projects. If you want to check another example just go to the Countries module.

  • That looks very good - thanks. However it appears that this API is new to D8 right? The D7 API looks very different for custom fields. Do you think this approach is possible in D7 as well? – testalino Nov 9 '18 at 13:52
  • @testalino I added an example for Drupal 7 – Adrian Cid Almaguer Nov 9 '18 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.