I have a config form with two select fields. The first one(vocabulary) gets populated automatically without problems with options taken from the available vocabularies for users to pick up one. The problem comes now, the second select field should be automatically populated with taxonomy terms based con the chosen vocabulary...but it DOES NOT work ($form_state->getValue('vocabulary';). My quesion is: is it posible to get the value of other fields right inside the buildForm() method without having to save configuration in submitForm() and later get the value from saved configuration?

Here is my buildForm() method:

 public function buildForm(array $form, FormStateInterface $form_state) {

    //get all vocabularies available and store their vid in an array(to be passend to select htmm form element)
    //we store vid instead of label because the latest can change at any moment
    $vocabulary = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple();
    $voc_options = ['--none--'];
    foreach ($vocabulary as $voc) {
      // $voc is here an entity object of type Drupal\taxonomy\Entity\Vocabulary;
      // convert the entity object to array and then access the value of vid(should be a better way)
      $voc_options[] = $voc->toArray()['vid'];

    $form['vocabulary'] = [
      '#type' => 'select',
      '#title' => $this->t('Select vocabulary'),
      '#options' => $voc_options,

    //$vtarget = $form_state->getValue('vocabulary'); // does not work
    $vtarget = 'authors'; // works
    $terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadByProperties(['vid' => $vtarget]);
    //$terms = $this->entityTypeManager->getStorage('taxonomy_term')->loadTree($vtarget);
    $sections = [];
    foreach ($terms as $term) {
      $sections[] = $term->label();

    $form['section'] = [
      '#type' => 'select',
      '#title' => $this->t('Select section'),
      '#options' => $sections,

    return parent::buildForm($form, $form_state);

1 Answer 1


Yes, it is possible.

What you need is adding the #ajax property to your vocabulary form element.
A good place to look for an example is the Examples for Developers project, which contains the DependentDropdown class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.