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

查看:21
本文介绍了如何在 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.创建表单

看这里:Form API 简介.

your_module/src/Form 下创建表单.在上面的链接中有更多详细信息.

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

3.创建块并渲染表单

看这里:创建自定义块.

your_module/src/Plugin/Block/ 下,您创建将呈现表单的块.

under your_module/src/Plugin/Block/ you create the block which will render the form.

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

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

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

return $renderArray;

注意:你不需要用 $renderArray 包裹 $builtForm,你可以只返回 $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天全站免登陆