如何以编程方式从自定义表单添加节点(表单api),然后在保存后重定向到该节点?
在寻找半天后找出以下答案。
希望这对其他人有用!
答案 0 :(得分:1)
'类型'是节点机器名称
'标题'是您要为新节点提供的标题
您可以添加更多' field_names'在您的节点中使用
use Drupal\node\Entity\Node;
use Drupal\Core\Url;
public function submitForm(array &$form, FormStateInterface $form_state) {
$newCompanyNode = Node::create([
'type' => 'company',
'title' => $form_state->getValue('company'),
//'field_name' => $value,
]);
$newCompanyNode->save();
drupal_set_message('Your company has been registered.', 'status');
$url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $newCompanyNode->id()]);
return $form_state->setRedirectUrl($url);
}
有关如何设置自定义表单的详细信息: https://www.drupal.org/docs/8/api/form-api/introduction-to-form-api