I'm trying to render taxonomy add term form inside content type ('article') add form. (node/add/article)

In hook_form_alter, I called this callback function on abutton.

public function _cvv_ajax_callback(VocabularyInterface $taxonomy_vocabulary) {
    $taxonomy_vocabulary = 'cvv';
    $term = \Drupal::entityManager()->getStorage('taxonomy_term')->create(['vid' => $taxonomy_vocabulary->id()]);
    return \Drupal::entityFormBuilder()->getForm($term);

But this is not return form in my node/add page, rather giving me error:

error php Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type taxonomy_term in Drupal\Core\Entity\ContentEntityStorageBase->doCreate()

I tried

if ($vocabulary = taxonomy_vocabulary_machine_name_load('jenis_dokumen')) {
  module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  $edit = array();
  $taxonomy_form = drupal_get_form('taxonomy_form_term', $edit ,$vocabulary);
  return drupal_render($taxonomy_form);

but this is also not working in Drupal 8.

Also tried:

 $form = \Drupal::formBuilder()->getForm('\Drupal\taxonomy\Controller\TaxonomyController::addForm','plan_features');

didn't worked :'(

Lastly I tried Inline Entity Form , the problem with module is, when a add term form get rendered inside node type form and if you do

kint($form_id); inside hook_form_alter, you won't get any form id for this inline form. Hence if you want to add custom validations for the term title, it won't be achievable even by hooks this module provides.

Please suggest a way to do this.

  • What was the error message of the second one? – ssibal Aug 28 '17 at 9:49

Have you tried this module? It's pretty much what you could need:

Inline Entity Form

  • 1
    This is a good solution, Inline Entity Form is on by default on D8 Lightning distribution. – Drupal de Comptoir Aug 28 '17 at 10:00
  • But altering inline entity form is also a big trouble. Because this module provides 3 hooks. So I want to add custom validation on term name, it is not achievable through these hooks. – Sugandh Khanna Aug 30 '17 at 11:33
  • Also, there's no way even to get the form id of the form rendered by "Inline Entity form." – Sugandh Khanna Aug 30 '17 at 11:44

Your first solution should work. Use the name of the vocabulary instead of the id ..

$term = \Drupal::entityManager()->getStorage('taxonomy_term')->create(['vid' => $taxonomy_vocabulary->label()]);

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.