使用hook_form_alter,您需要做两件事
1)确保它是节点表单2)向每个提交按钮添加一个自定义提交处理程序。
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['#entity_type']) && $form['#entity_type'] == 'node') {
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'mymodule_node_form_submit';
}
}
}
}
然后对于提交功能,您可以使用所需的任何逻辑。您可以与NodeForm :: save进行比较,在NodeForm :: save中,它可以根据当前用户的访问权限将您发送到规范的节点页面或首页。
如果要更改此行为以使其保留在当前节点窗体上,则可以执行以下操作:
function mymodule_node_form_submit($form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
if ($node->id()) {
if ($node->access('edit')) {
$form_state->setRedirect(
'entity.node.edit_form',
['node' => $node->id()]
);
}
else {
$form_state->setRedirect('<front>');
}
}
}
如果要使用自定义登录页面,只需将重定向替换为已经使用的代码:
$form_state->setRedirect('custom.landing.page');
请注意,当存在“目标” $ _GET参数时(例如在/ admin / content页面上),该值不会被覆盖。
要从/ admin / content页面中删除目标参数,您需要取消选中该视图字段中“内容:操作链接(操作)”下的“目标”复选框。
If saving is an option, privileged users get dedicated form submit buttons to adjust the publishing status while saving in one go. @todo This adjustment makes it close to impossible for contributed modules to integrate with "the Save operation" of this form. Modules need a way to plug themselves into 1) the ::submit() step, and 2) the ::save() step, both decoupled from the pressed form button.