0
我有一個分類選項列表make我選擇說豐田。分類列表依賴於從另一個分類列表中選擇,drupal 8
- 我想第二個分類選項列表只與豐田模型(例如花冠,hilux等...)。
- 當我選擇奔馳第二列表將包含隨後C級,ML等...
我已經從本地主機XAMPP谷歌的例子創建實體車輛,窗口10
在我的車輛形式我可以填充第一個列表。但第二個看起來是空的。
這是我的代碼。請幫助:
public function buildForm(array $form, FormStateInterface $form_state, $params = NULL) {
$options = array();
$tax = "make";
$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($tax, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
foreach ($terms as $term) {
$options[] = $term->name;
}
$form['make'] = array(
'#type' => 'select',
'#title' => t('Make'),
'weight' => 0,
'#options' => $options,
'#ajax' => array(
'callback' => [$this, 'changeOptionsAjax'],
'wrapper' => 'model_wrapper',
),
);
$form['model'] = array(
'#type' => 'select',
'#title' => t('Model'),
'weight' => 1,
'#options' => $this->getOptions($form_state),
'#prefix' => '<div id="model_wrapper">',
'#suffix' => '</div>',
);
return $form;
}
public function getOptions(FormStateInterface $form_state) {
$options = array();
if ($form_state->getValue('make') == "Benz") {
$tax="benz";
}
elseif ($form_state->getValue('make') == "BMW") {
$tax="bmw";
}
elseif ($form_state->getValue('make') == "Toyota") {
$tax="toyota";
}
else {
$tax="title";
// title is just another taxonomy list I'm using as default if make is not found
}
$terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($tax, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
foreach ($terms as $term) {
$options[] = $term->name;
}
return $options;
}
public function changeOptionsAjax(array &$form, FormStateInterface $form_state) {
return $form['model'];
}
你救了我,現在爲我工作。起初有錯誤,但添加了 – Clemabiz
你已經救了我很多,現在工作謝謝。起初有錯誤,但添加了「使用Drupal \ Core \ Form \ FormStateInterface;」並將「class vehicleForm extends FormBase」改爲「class VehicleForm extends ContentEntityForm」。 NB。我還將「車輛」改回「車輛」,因爲我的整個主體文件都涉及此實體。我現在正在研究如何從表單中保存實體(車輛)......因爲這不是將實體保存到數據庫中。感謝批號 – Clemabiz
樂於幫助,並歡迎來到Stack Overflow。如果此答案或任何其他人解決了您的問題,請將其標記爲已接受。它將幫助人們在頁面上看到好的答案,這也有助於撰稿人將注意力集中在仍然沒有答案的舊版SO上。 –