How to add autocomplete field to custom form which will fetch data from taxanomy?

I have created a custom form using FormBase. In which there are 3 fields City, State & Country. I’ve created taxanomy for these 3 fields. Now i want to create autocomplete field which will fetch data from database from taxanomy and also add the the term to taxanomy if not exist in the table. Please help me with these. Below is the code :

HotelForm.php under src/Form directory

/** * @file * Contains Drupalhotel_formFormHotelForm */ namespace Drupalhotel_formForm; use DrupalCoreFormFormBase; use DrupalCoreFormFormStateInterface; use DrupalCoreUrl; class HotelForm extends FormBase {

public function getFormId() {     return 'hotel'; }   public function buildForm(array $form, FormStateInterface $form_state) {      $form['hotel_title'] = array(         '#type' => 'textfield',         '#size' => 50,         '#title' => t('Hotel Name'),         '#required' => TRUE,         '#placeholder' => t('Hotel Name'),     );       $form['hotel_city'] = array(         '#type' => 'entity_autocomplete',   '#target_type' => 'node',         '#size' => 50,         '#title' => 'City',         '#required' => TRUE,         '#placeholder' => t('City'),       );      $form['hotel_state'] = array(         '#type' => 'textfield',         '#size' => 50,         '#title' => t('State'),         '#required' => TRUE,         '#placeholder' => t('State'),     );      $form['hotel_country'] = array(         '#type' => 'textfield',         '#size' => 50,         '#title' => t('Country'),         '#required' => TRUE,         '#placeholder' => t('Country'),     );      $form['hotel_mobile'] = array(         '#type' => 'number',         '#title' => t('Mobile Number'),         '#required' => TRUE,         '#placeholder' => t('Mobile Number'),     );      $form['hotel_phone'] = array(         '#type' => 'number',         '#title' => t('Phone Number'),         '#required' => TRUE,         '#placeholder' => t('Phone Number'),     );      $form['submit'] = array(         '#type' => 'submit',         '#value' => t('Submit'),         '#attributes' => array(         'class' => array('btn btn-submit'),             ),     );     return $form;  }    public function validateForm(array &$form, FormStateInterface $form_state) {   }  public function submitForm(array &$form, FormStateInterface $form_state) {      $name = $form_state->getValue('hotel_title');     $city = $form_state->getValue('hotel_city');     $state = $form_state->getValue('hotel_state');     $country = $form_state->getValue('hotel_country');     $phone = $form_state->getValue('hotel_phone');     $mobile = $form_state->getValue('hotel_mobile');      echo $name;     $uuid = Drupal::service('uuid');      // $uuid->isValid($uuid_to_validate);      $query = db_insert('node')      ->fields(array('type','uuid','langcode'))      ->values(array(         'type' => 'hotels',         'uuid' => $uuid->generate(),         'langcode' => 'en',         ))     ->execute();      $user = DrupaluserEntityUser::load(Drupal::currentUser()->id());     $uid= $user->get('uid')->value;      $query1 = db_insert('node_revision')     ->fields(array('nid','langcode','revision_timestamp','revision_uid'))     ->values(array(         'nid' => $query,         'langcode' => 'en',         'revision_timestamp' => REQUEST_TIME,         'revision_uid' => $uid,         ))     ->execute();      $query2 = db_update('node')     ->fields(array(         'vid' => $query1,         ))         ->condition('nid',$query)         ->execute();      $query3 = db_insert('node_field_data')     ->fields(array(         'nid' => $query,         'vid' => $query1,         'type' => 'hotels',         'langcode' => 'en',         'title' => $name,         'uid' => $uid,         'status' => 0,         'created' => REQUEST_TIME,         'changed' => REQUEST_TIME,         'promote' => 0,         'sticky' => 0,         'revision_translation_affected' => 1,         'default_langcode' => 1,         ))     ->execute();      $query4 = db_insert('node_field_revision')     ->fields(array(         'nid' => $query,         'vid' => $query1,         'langcode' => 'en',         'title' => $name,         'uid' => $uid,         'status' => 0,         'created' => REQUEST_TIME,         'changed' => REQUEST_TIME,         'promote' => 0,         'sticky' => 0,         'revision_translation_affected' => 1,         'default_langcode' => 1,         ))     ->execute();      $query5 = db_insert('node__field_city')     ->fields(array(         'bundle' => 'hotels',         'deleted' => 0,         'entity_id' => $query,         'revision_id' => $query1,         'langcode' => 'en',         'delta' => 0,         'field_city_value' => $city,         ))     ->execute();      $query6 = db_insert('node_revision__field_city')     ->fields(array(         'bundle' => 'hotels',         'deleted' => 0,         'entity_id' => $query,         'revision_id' => $query1,         'langcode' => 'en',         'delta' => 0,         'field_city_value' => $city,         ))     ->execute();      $query7 = db_insert('node__field_state')     ->fields(array(         'bundle' => 'hotels',         'deleted' => 0,         'entity_id' => $query,         'revision_id' => $query1,         'langcode' => 'en',         'delta' => 0,         'field_state_value' => $state,         ))     ->execute();      $query8 = db_insert('node_revision__field_state')     ->fields(array(         'bundle' => 'hotels',         'deleted' => 0,         'entity_id' => $query,         'revision_id' => $query1,         'langcode' => 'en',         'delta' => 0,         'field_state_value' => $state,         ))     ->execute();      $query9 = db_insert('node__field_country')     ->fields(array(         'bundle' => 'hotels',         'deleted' => 0,         'entity_id' => $query,         'revision_id' => $query1,         'langcode' => 'en',         'delta' => 0,         'field_country_value' => $country,         ))     ->execute();      $query10 = db_insert('node_revision__field_country')     ->fields(array(         'bundle' => 'hotels',         'deleted' => 0,         'entity_id' => $query,         'revision_id' => $query1,         'langcode' => 'en',         'delta' => 0,         'field_country_value' => $country,         ))     ->execute();      drupal_set_message(t('Thank you')); } 

}

This is my routing file:

hotel_form.hotel_form:   path: '/add-hotel'   defaults:     _title: 'Hotel Form'     _form: 'Drupalhotel_formFormHotelForm'   requirements:     _permission: 'access content'

Thanks

Sponsored by SupremePR
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

How to add autocomplete field to custom form which will fetch data from taxanomy?

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.