Hello,

I am very new in Drupal and sorry in advance if my questions seem to be obvious at most of you.

I would like to create a form where the authenticated users can make requests.

I need to get the location from their IP address and to automatically fill in fields (Country, City) within the form,

1. what it the best way to consume a Web Service in Drupal?, is there a complete example somewhere that could help me to know how to do it step by step?

2. how to automatically fill in the fields in the form?

I did it in the past using PHP and Jquery but I dont know how to do in Drupal

Thank you in advance for your help

Comments

Jaypan’s picture

Drupal 7 overview

For creating a form, you have two options:

1) Use the webform module. This module is made for creating forms, it's a form builder.

2) Create your own module using the Form API. Takes more time, and is more difficult, but gives you complete control over the form.

For the web service, if you use the webform module, you'll have to use hook_form_alter() to add your own submit handler to the form. If you create your own form, you'll use the default submit handler.

In the submit handler, you can use drupal_http_request() to make requests to the service. This is an all-purpose external request handler that is as flexible as you need it to be.

sanfisa’s picture

Thank you, I just realised that you described the solution for Drupal 7, my question was for Drupal 8

I

Jaypan’s picture

Sorry, I didn't see that.

Everything is the same except that the webform module doesn't exist, and you'll need to use Guzzle 8https://www.drupal.org/node/1862446) instead of drupal_http_request().

sanfisa’s picture

At https://github.com/vilepickle/drupal-8-custom-guzzle-request

there is an example but does not work for me.

Do you know where I can find a skeleton with what should be created ?

Thank you in advance

alarhby’s picture

you may have a look at this model 

https://www.drupal.org/project/wsdata