Drupal 8:以编程方式创建节点并使用表单 api 重定向到它
Drupal 8: programmatically create node and redirect to it using form api
如何以编程方式从自定义表单(表单 api)添加节点,然后在保存后重定向到该节点?
找了半天找到了下面的答案。
希望这对其他人有用!
'type'为节点机器名
'title'是你要给新节点的标题
您可以添加更多 '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
如何以编程方式从自定义表单(表单 api)添加节点,然后在保存后重定向到该节点?
找了半天找到了下面的答案。
希望这对其他人有用!
'type'为节点机器名
'title'是你要给新节点的标题
您可以添加更多 '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