Вы можете достичь этого, используя #ajax в форме alter.Кроме того, вам нужно создать одну таксономию с отношениями родитель-потомок, а не две отдельные.
Использовать модуль Contribute Простой иерархический выбор
ИЛИ
Ниже приведен пример кода:
function example_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
switch ($form_id) {
case 'example_form':
$states = [];
$vid = 'states';
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, NULL, FALSE);
foreach ($terms as $term) {
if ($term->depth == 0)
$states[$term->tid] = $term->name;
}
$form['states'] = [
'#title' => t('States'),
'#type' => 'select',
'#options' => $states,
'#required' => TRUE,
'#ajax' => [
'callback' => 'getCityList',
'event' => 'change',
'wrapper' => ['autocomplete_city_container'],
'method' => 'replace',
'effect' => 'slide',
'progress' => [
'type' => 'throbber',
'message' => t('Fetching city...'),
],
]
];
$form['city_alter'] = [
'#type' => 'container',
'#attributes' => ['id' => ['autocomplete_city_container']],
];
$form['city_alter']['city'] = array(
'#title' => t('City'),
'#type' => 'select',
'#required' => TRUE,
'#options' => [],
);
if (!empty($form_state->getValue('states'))) {
$cities = [];
$stateTid = $form_state->getValue('states');
$vid = 'city';
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $stateTid, NULL, FALSE);
foreach ($terms as $term) {
$cities[$term->tid] = $term->name;
}
$form['city_alter']['city'] = array(
'#title' => t('Size'),
'#type' => 'select',
'#required' => TRUE,
'#options' => $cities,
);
}
break;
}
}
function getCityList(array &$form, FormStateInterface $form_state) {
return $form['city_alter'];
}