通过 Drupal 8 中的控制器创建用于呈现自定义表单的自定义模块
Create custom module for render custom forms through a controller in Drupal 8
我需要通过自定义 Drupal 8 模块中的控制器呈现使用 Drupal\Core\Form\FormBase 和 Drupal\Core\Form\FormStateInterface 创建的自定义表单。是否有任何指导或参考可以遵循?
实际上我试图通过控制器直接渲染表单。但是这两种方式都行不通。只渲染提交按钮。我也参考了 drupal 8 文档。但我找不到解决方案。请友好地在下面找到我的编码示例。如果有什么不妥。请指正。
my_module.routing.yml
partner.content:
path: '/partner'
defaults:
_controller: '\Drupal\partner\Controller\PartnerController::add'
_title: 'Add Partner'
requirements:
_permission: 'access content'
partner.addform:
path: '/partner/add'
defaults:
_form: '\Drupal\partner\Form\AddForm'
_title: 'Add Partner'
requirements:
_permission: 'access content'
AddForm.php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class AddForm extends FormBase
{
/**
* Returns form id
*
* @return string
*/
public function getFormId(): string
{
return 'my_module_add_form';
}
/**
* Build form array
*
* @param array $form
* @param FormStateInterface $formState
* @return array
*/
public function buildForm(array $form, FormStateInterface $form_state): array
{
// First name
$form['first_name'] = [
'#type' => 'textField',
'#title' => t('First Name'),
'#required' => true,
];
// Other input fields...
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save Changes'),
'#button_type' => 'primary',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {}
public function submitForm(array &$form, FormStateInterface $form_state) {}
}
MyModuleController.php
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\my_module\Form\AddForm;
class MyModuleController extends ControllerBase
{
public function add()
{
$addForm = new AddForm();
$form = \Drupal::formBuilder()->getForm($addForm);
return [
'#theme' => 'form_my_module_add',
'#form' => $form,
];
}
}
很高兴在 Hemantha Dhanushka 的评论中找到解决方案。
为了明确这个问题有正确答案,我在这里通过了验证评论。
I would recommend you to use the first approach (using routing::_form instead
of Controller). Also, it seems you use the wrong #type
for your
first_name
field. Try textfield
instead of textField
.
此外,对于想要更进一步的人,这里有一些链接可以实现适当的
routing::_form 将表单公开为页面而不是使用控制器的方法:https://www.valuebound.com/resources/blog/step-by-step-method-to-create-a-custom-form-in-drupal-8.
对于寻求有关现有表单元素参考(textfield
、checkboxes
、entity_autocomplete
、...)的更多帮助的人,这里有一篇优秀的up-to-date文章https://drupalize.me/tutorial/form-element-reference?p=2766
您可以使用buildForm()
方法。检查下面的代码示例:
public function add()
{
$form_state = new Drupal\Core\Form\FormState();
$form_state->setRebuild();
$form = \Drupal::formBuilder()->buildForm('Drupal\my_module\Form\AddForm', $form_state);
return [
'#theme' => 'form_my_module_add',
'#form' => $form,
];
}
我需要通过自定义 Drupal 8 模块中的控制器呈现使用 Drupal\Core\Form\FormBase 和 Drupal\Core\Form\FormStateInterface 创建的自定义表单。是否有任何指导或参考可以遵循?
实际上我试图通过控制器直接渲染表单。但是这两种方式都行不通。只渲染提交按钮。我也参考了 drupal 8 文档。但我找不到解决方案。请友好地在下面找到我的编码示例。如果有什么不妥。请指正。
my_module.routing.yml
partner.content:
path: '/partner'
defaults:
_controller: '\Drupal\partner\Controller\PartnerController::add'
_title: 'Add Partner'
requirements:
_permission: 'access content'
partner.addform:
path: '/partner/add'
defaults:
_form: '\Drupal\partner\Form\AddForm'
_title: 'Add Partner'
requirements:
_permission: 'access content'
AddForm.php
namespace Drupal\my_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class AddForm extends FormBase
{
/**
* Returns form id
*
* @return string
*/
public function getFormId(): string
{
return 'my_module_add_form';
}
/**
* Build form array
*
* @param array $form
* @param FormStateInterface $formState
* @return array
*/
public function buildForm(array $form, FormStateInterface $form_state): array
{
// First name
$form['first_name'] = [
'#type' => 'textField',
'#title' => t('First Name'),
'#required' => true,
];
// Other input fields...
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Save Changes'),
'#button_type' => 'primary',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {}
public function submitForm(array &$form, FormStateInterface $form_state) {}
}
MyModuleController.php
<?php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\my_module\Form\AddForm;
class MyModuleController extends ControllerBase
{
public function add()
{
$addForm = new AddForm();
$form = \Drupal::formBuilder()->getForm($addForm);
return [
'#theme' => 'form_my_module_add',
'#form' => $form,
];
}
}
很高兴在 Hemantha Dhanushka 的评论中找到解决方案。
为了明确这个问题有正确答案,我在这里通过了验证评论。
I would recommend you to use the first approach (using routing::_form instead of Controller). Also, it seems you use the wrong
#type
for yourfirst_name
field. Trytextfield
instead oftextField
.
此外,对于想要更进一步的人,这里有一些链接可以实现适当的 routing::_form 将表单公开为页面而不是使用控制器的方法:https://www.valuebound.com/resources/blog/step-by-step-method-to-create-a-custom-form-in-drupal-8.
对于寻求有关现有表单元素参考(textfield
、checkboxes
、entity_autocomplete
、...)的更多帮助的人,这里有一篇优秀的up-to-date文章https://drupalize.me/tutorial/form-element-reference?p=2766
您可以使用buildForm()
方法。检查下面的代码示例:
public function add()
{
$form_state = new Drupal\Core\Form\FormState();
$form_state->setRebuild();
$form = \Drupal::formBuilder()->buildForm('Drupal\my_module\Form\AddForm', $form_state);
return [
'#theme' => 'form_my_module_add',
'#form' => $form,
];
}