Taxonomy form autocomplete, Create taxonomy term field with FAPI | Drupal 8

Taxonomy form autocomplete, Create taxonomy term field with FAPI

Submitted by editor on Mon, 03/26/2018 - 11:07
Question

How to create a taxonomy term autocomplete field.

Simple taxonomy autocomplete field.

Example:
$form['tagtest'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'taxonomy_term',
  '#title' => 'Taxonomy Term',
];

Field with default value

Example : Default TID = 1
$form['tagtest'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'taxonomy_term',
  '#title' => 'Taxonomy Term',
  '#default_value' => \Drupal\taxonomy\Entity\Term::load(1),
];

With a selected vocabulary

Example : 'tags'
$form['tagtest'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'taxonomy_term',
  '#title' => 'Taxonomy Term',
  '#selection_settings' => [
    'target_bundles' => ['tags'],
  ],
];

 

Auto create a term if not exist

Example:
$form['tagtest'] = [
  '#type' => 'entity_autocomplete',
  '#target_type' => 'taxonomy_term',
  '#title' => 'Taxonomy Term',
  '#autocreate' => [
    'bundle' => 'tags', // Required. The bundle name for the new entity.
    'uid' => 1, // Optional. The user ID for the new entity
  ],
];

In this case the returned value is not only a string, it can be a Term.
If new term created, you must save it.
Example:
$tag = $form_state->getValue('tagtest');
if (empty($tag)) {
  drupal_set_message("Tag is empty, nothing to do");
}
elseif (is_string($tag)) {
  drupal_set_message("A term selected, tid = $tag");
}
elseif (isset($tag['entity']) && ($tag['entity'] instanceof \Drupal\taxonomy\Entity\Term)) {
  $entity = $tag['entity'];
  $entity->save();
  drupal_set_message("A new term : " . $entity->id() . " : " . $entity->label());
}

 

 

Comments

robertwright

Sat, 08/11/2018 - 12:12

  I got more information about drupal by reading this. Thanks for sharing this one with us. It is good event and gives more thoughtful matter for this things and inspiring things. Since I am a writer I really impressed with the information. If you want writing service, please contact Custom Essay Writing Service one of the best writing services available. The better service that provides detailed and effective information related to educational basis.

R HILDAN M (not verified)

Fri, 12/14/2018 - 16:59

ling 4 agen steel sheet pile distributor astm a283 c distributor pipa boiler distributor plat abrex 400 distributor plat asme516 grade70 distributor plat astm a285 c distributor plat astm516 70 distributor plat boiler distributor plat hb 400 distributor plat high strenght distributor plat high tensile sm490 yb Distributor plat kapal besi baja Distributor plat kapal besi baja undangan pernikahan klub bola Agen besi beton undangan pernikahan kalender Toko Wiremesh Distributor Pipa Besi baja Schedule sch 80 Distributor besi unp baja Supplier besi beton perwira Harga plat kapal besi baja Harga plat kapal besi baja undangan pernikahan cetak undangan pernikahan coklat Toko Besi Pipa Baja Schedule 80 Supplier Pipa Besi baja Schedule sch 80 Harga besi unp baja Supplier besi beton PAS Jual plat kapal besi baja Jual plat kapal besi baja Agen stainless steel www.pusatbesibaja.co.id undangan pernikahan arab Toko Besi Pipa Baja Schedule 40 Agen Pipa Besi baja Schedule sch 80 Jual besi unp baja Supplier besi beton master steel ms Supplier plat kapal besi baja Supplier plat kapal besi baja Agen atap spandek www.pusatbesibaja.com undangan pernikahan kalender Toko Pipa Besi Hitam Medium Toko Pipa Besi baja Schedule sch 80 Supplier besi unp baja Supplier besi beton ksty Toko plat kapal besi baja Toko plat kapal besi baja Agen baja ringan undangan pernikahan custom Toko Besi Wf Jual Pipa Besi baja Schedule sch 80 Toko besi unp baja Supplier besi beton krakatau steel Agen plat kapal besi baja Agen plat kapal besi baja Agen bondek Toko besi beton Sni Ulir Polos polos ulir undangan pernikahan klasik Toko Besi H beam Harga Pipa Besi baja Schedule sch 80 Agen besi unp baja Supplier besi beton jcac Pabrik plat kapal besi baja Pabrik plat kapal besi baja Agen Plat kapal besi baja bki krakatau steel Toko besi beton sii undangan pernikahan batik Toko Besi Beton Pabrik Pipa Besi baja Schedule sch 80 Pabrik besi unp baja Supplier besi beton interworld steel is Distributor besi siku baja Distributor besi siku baja Agen besi siku Agen besi Wf Baja undangan pernikahan www.sentrabesibaja.com Distributor Pipa Besi baja Distributor besi h beam baja Supplier besi beton gunung garuda

Add new comment