I created a form via Drupal 8 API, and I am creating a dropdown of a taxonomy terms which has a parent-child hierarchy. But when i select the taxonomy term, it displays term's parent's tid. This is how i try to display the dropdown:
$option = [];
$terms = \Drupal::service('entity_type.manager')->getStorage("taxonomy_term")->loadTree('collections', $parent = 0, $max_depth = 3, $load_entities = FALSE);
foreach ($terms as $term) {
if($term->depth == 0){
$term = $term->name;
}
elseif ($term->depth == 1) {
$term = '-'.$term->name;
}
elseif ($term->depth == 2){
$term = '--'.$term->name;
}
elseif ($term->depth == 3){
$term = '---'.$term->name;
}
elseif ($term->depth == 4){
$term = '----'.$term->name;
}
$option[] = $term;
}
$form['collection'] = array(
'#type' => 'select',
'#title' => t('In Collection:'),
'#empty_option' => t(' '),
'#options' => $option,
);
I have been trying to get the term's tid not the parent's tid, can anyone point out what am doing wrong? Note:I am not super experienced with drupal 8