如何在Drupal 8中使用块模块创建表单? [英] How to create a form using block module in drupal 8?

查看:128
本文介绍了如何在Drupal 8中使用块模块创建表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Drupal 8 中使用块模块构建表单。我知道可以在 Drupal 7中构建表单,但是在Drupal 8中似乎相同。

I want build a form using a block module in Drupal 8. I am aware of building the forms in Drupal 7 but the same seems to be different in Drupal 8.

请求任何使用drupal8自定义表单的人都可以为我提供帮助。

Request anyone who has worked on drupal8 custom forms as block to help me.

推荐答案

您的问题非常模糊,因为我不知道知道您已经对Drupal 8中的模块,表单和块有多少了解。因此,这是一个小指南,该怎么做,进一步详细了解如何做这些事情,对于此答案可能会显得过头。

Your question is very vague, as I don't know how much you already know about modules, forms and blocks in Drupal 8. So here is a small guide what to do, further information on how to do stuff in detail would be overkill for this answer.

1。创建一个新模块并启用它

查看此处:命名并放置Drupal 8模块

基本上,您会创建模块文件夹和模块信息yml文件,以使Drupal知道该模块。 。然后,使用drush或Drupal中的管理区域启用它。

Basically you create the module folder and the module info yml file to let Drupal know about the module. Then you enable it using drush or the admin area in Drupal.

2。创建表单

查看此处:表单API简介

中创建表单。在上面的链接中有更多详细信息。

under your_module/src/Form you create the form. More details in the link above.

3。创建块并呈现表单

查看此处:创建自定义块

your_module / src / Plugin / Block /

这个想法基本上是(根据Henrik的建议更新的代码):

The idea is basically (code updated with suggestion from Henrik):

$builtForm = \Drupal::formBuilder()->getForm('Drupal\your_module\Form\Your‌​Form');
$renderArray['form'] = $builtForm;

return $renderArray;

注意:您不需要包装 $ builtForm $ renderArray ,您可以只返回 $ builtForm 即可。我只是个人喜欢这样做,因为通常我需要在最终的渲染数组中添加其他内容,例如标记,缓存设置或库等。

Note: You don't need to wrap the $builtForm with the $renderArray, you can return just the $builtForm and be fine. I just personally like to do it that way, because often times I need to add something else to the final render array like some markup, cache settings or a library etc.

4。将区块放置

将区块放置在所需区域中。完成。

Place the block in the desired region(s). Done.

这篇关于如何在Drupal 8中使用块模块创建表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆