1

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 \Drupal\hotel_form\Form\HotelForm */ namespace Drupal\hotel_form\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; 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 = \Drupal\user\Entity\User::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: 'Drupal\hotel_form\Form\HotelForm'
  requirements:
    _permission: 'access content'

Thanks

  • Share the code that you have. – Berdir Aug 1 '16 at 10:55
  • Hi Berdir,Please check the code, i've updated the question... – Sachin Aug 1 '16 at 11:04
  • 1
    Not sure where to start. never save nodes by hand into the tables, use the API, it's so much easier. And next, I don't understand why you don't just use the standard node add form that's automatically build for you. It has auto-complete for all fields built in based on your configuration, you can always customize it a bit if necessary. – Berdir Aug 1 '16 at 12:06
0

In order to know how to use the form element 'entity_autocomplete', either have a look at the form api: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Element%21EntityAutocomplete.php/class/EntityAutocomplete/8.2.x Or search in core for '#type' => 'entity_autocomplete'. For example, the CommentForm uses this form element for users this way:

 $form['author']['uid'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'user',
  '#default_value' => $owner->isAnonymous() ? NULL : $owner,
  // A comment can be made anonymous by leaving this field empty therefore
  // there is no need to list them in the autocomplete.
  '#selection_settings' => ['include_anonymous' => FALSE],
  '#title' => $this->t('Authored by'),
  '#description' => $this->t('Leave blank for %anonymous.', ['%anonymous' => $config->get('anonymous')]),
  '#access' => $is_admin,
];

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.