有谁知道如何在 Drupal 8 中更改段落(ajax)后端表单中的字段?我想禁用一个字段,但让它保持可见。
谢谢
function hook__form_FORM_ID_alter(&$form,\Drupal\Core\Form\FormStateInterface
$form_state, $form_id) {
//output your form structure to know what to target in the form array($form[])
#kint( $form['title']);
$form['title']['#disabled'] = TRUE;
}
上面的代码禁用了要修改的“FORM_ID”中的标题字段(Drupal 8.5)。
您可以使用 hook_form_alter() 或 hook_form_FORM_ID_alter() 禁用表单字段。
我总是建议你使用 hook_form_FORM_ID_alter()。假设 test 是您的模块名称,而 user_register_form 是表单的 ID。
test_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['fieldname'] = array(
'#type' => 'textfield',
'#title' => t('Text label'),
'#attributes' => array('disabled' => 'disabled'),
);
}
快乐编码!!!