在登录表单上添加一个按钮 (D8)
Add a button on the login form (D8)
是否可以在基本登录表单上添加按钮?
function alter_form_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
if ($form_id == 'user_login_form') {
##ADD BUTTONS ???
$form['#validate'] = ['test_validate'];
$form['actions']['submit']['#submit'][] = 'custom_submit_method';
}
}
您可以使用以下代码在 actions
包装器中添加一个按钮
$form['actions']['custom_submit'] = [
'#type' => 'submit',
'#name' => 'custom_submit',
'#value' => t('My custom submit button'),
];
如果您想要区分这两个点击,例如,如果您需要在使用 custom_submit
时执行其他操作,那么您将需要访问 custom_submit_method
中的 $form_state->getTriggeringElement();
提交处理程序。
function custom_submit_method(array $form, FormStateInterface $form_state){
$trigger = $form_state->getTriggeringElement();
if ($trigger['#name'] === 'custom_submit') {
// ...
}
}
您可能会找到更多关于
的文档
::getTriggeringElement
的用法:https://drupal.stackexchange.com/questions/223289/how-to-get-triggering-element
- 自定义提交处理程序的用法:
https://drupal.stackexchange.com/questions/223342/add-a-custom-submission-handler-to-a-form
希望我的回答对您有所帮助
是否可以在基本登录表单上添加按钮?
function alter_form_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
if ($form_id == 'user_login_form') {
##ADD BUTTONS ???
$form['#validate'] = ['test_validate'];
$form['actions']['submit']['#submit'][] = 'custom_submit_method';
}
}
您可以使用以下代码在 actions
包装器中添加一个按钮
$form['actions']['custom_submit'] = [
'#type' => 'submit',
'#name' => 'custom_submit',
'#value' => t('My custom submit button'),
];
如果您想要区分这两个点击,例如,如果您需要在使用 custom_submit
时执行其他操作,那么您将需要访问 custom_submit_method
中的 $form_state->getTriggeringElement();
提交处理程序。
function custom_submit_method(array $form, FormStateInterface $form_state){
$trigger = $form_state->getTriggeringElement();
if ($trigger['#name'] === 'custom_submit') {
// ...
}
}
您可能会找到更多关于
的文档::getTriggeringElement
的用法:https://drupal.stackexchange.com/questions/223289/how-to-get-triggering-element- 自定义提交处理程序的用法: https://drupal.stackexchange.com/questions/223342/add-a-custom-submission-handler-to-a-form
希望我的回答对您有所帮助