How to Create Custom Location Dropdown Block On Your Drupal Site
Here we are creating the Location Dropdown Block (Country-Region Dropdown) with the help of taxonomy terms.
Steps:-
First of all, We have to go on over Drupal Site and Create the Vocabulary Name ‘Location’:
structure/taxonomy/Location
Then Create Country as Parent Term And Region as Child Term accordingly :
Create a Custom Module For the Location Dropdown Block (We can also use any Existing Custom Module For Location Dropdown Block):
Create Custom Block File (PHP file):
src/Plugin/Block/LocationDropdownBlock.php
Here is the Example Code of .PHP file
Now we have to use any API to get the IP Address of the User:
Free API to get the IP Address Of the User:
1. https://ipapi.co/json
2 .https://rapidapi.com/ibrarasadbiz/api/ip-geo-location-find-ip-location-and-details/
3.https://rapidapi.com/xakageminato/api/ip-geolocation-ipwhois-io/-
Paid API to get the IP Address Of the User:
https://developers.google.com/maps/documentation/geolocation/get-api-key
we are using free API :–https://ipapi.co/json
Create a Form :-
src/Form/DemoappLocationBlockForm.php:
Here is the Example Code of .PHP file
Next, Go to Your Drupal Site and Install the Location Dropdown Block:
Place the Location Dropdown Block into the Header Region:
Go to structure/block layout/
We can now see the location of the user as per region:
If you still have any questions regarding the topic, drop your comments down below.