Drupal API call

I need to display country name in a select filed and display its city names in another select field according to selection change committed on country name field. i am using ajax for selection change event. This is what i have done so far. now i need to fetch city names from second api corresponding to the country name in the select field.

public function getFormId() {  return 'newmoduleform_ajax_form'; }  public function buildForm(array $form, FormStateInterface $form_state) {  // First API Call to populate country select field    $result = [];  $country_options = [];  $client = Drupal::httpClient();  // Fetch API values.   $request = $client->request('GET','https://restcountries.com/v2/all');  $result = Json::decode($request->getBody()->getContents());   foreach ($result as $item) {    $country_options[] = $item['name'];   }      if (empty($form_state->getValue('country_dropdown'))) { // Use a default value.   $selected_option = key($country_options);   }   else {   // Get the value if it already exists.   $selected_option = $form_state->getValue('country_dropdown');    }  $form['option_country_fieldset'] = [   '#type' => 'fieldset', ];  $form['option_country_fieldset']['country_dropdown'] = [   '#type' => 'select',   '#title' => $this->t('country'),   '#options' => $country_options,    // Bind an Ajax callback to the element.   '#ajax' => [     'callback' => '::instrumentDropdownCallback',     'wrapper' => 'state-fieldset-container',     'event' => 'change',   ], ];  $form['select_fieldset_container'] = [   '#type' => 'container',   '#attributes' => ['id' => 'state-fieldset-container'], ];  $form['select_fieldset_container']['select_fieldset'] = [   '#type' => 'fieldset',    ];  $form['select_fieldset_container']['select_fieldset']['select_dropdown'] = [   '#type' => 'select',   '#title' =>  $this->t('Country Name-'). ' ' . $country_options[$selected_option],   '#options' => static::getSecondDropdownOptions(), ]; return $form; }   public function submitForm(array &$form, FormStateInterface $form_state) {   }  public function instrumentDropdownCallback(array $form, FormStateInterface $form_state)  {  return $form['select_fieldset_container']; }  public static function getSecondDropdownOptions() { [![second api that have city name][1]][1] $result_second = []; $city_options = []; $client = Drupal::httpClient(); // Fetch API values.  $request = $client->request('GET','https://countriesnow.space/api/v0.1/countries'); $result_second = Json::decode($request->getBody()->getContents());  foreach ($result_second[data] as $item_second) {     // what to do here  }   // what to do here } 

first api that has city name

api that list country name

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Drupal API call

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.