forms - 如何在 Drupal 8 中删除表单树枝中的字段?
问题描述
在 Drupal 8 中,表单 twig 是这样的:
<form{{ attributes }}>
{{ children }}
</form>
我想显示除一个以外的所有字段。通常我们使用without
过滤器,但children
它是一个字符串,所以它不起作用。
<form{{ attributes }}>
{{ children|without('unwanted') }}
</form>
在 twig 表单中,我们可以访问element
作为数组的变量。但是,没有过滤器在这里不起作用。
<form{{ attributes }}>
{{ element|without('unwanted') }}
</form>
我知道我可以用 1 by 1 显示所有字段{{ element.field }}
,但是如果我的表单有新字段,则在我更新表单树枝之前不会打印它们。我想保持动态目的。
我不想更新 php 中的表单代码以保持树枝中的布局。
任何想法 ?
解决方案
我们找到了一个很好的答案:
- 创建 du 自定义模板
{#
/**
* @file
*
* Available variables
* - form
*
* @see template_preprocess_mycustomform()
*
* @ingroup themeable
*/
#}
{{ form }}
- 在主题函数中声明模板
'mycustomform' => [
'render element' => 'form',
],
- 将模板应用到表单
$form['#theme'] = 'mycustomform';
结果:在新模板中,我们可以使用without
twig 函数
{{ form|without('myfield') }}
推荐阅读
- java - 如何通过按钮打开的对话框中的操作按钮打开另一个活动?
- python - PyInstaller: PermissionError: [Errno 13] Permission denied: 'C:\\Users\\user123\\AppData\\Roaming\\pyinstaller\\bincache00_py37_32bit
- pandas - 如何在熊猫中做系列运算符?
- c++ - 结构中的递归shared_ptr在重置时抛出错误
- javascript - 当用户使用 Javascript 在外部点击时关闭下拉菜单
- ios - swift:无法将类型“()”的值转换为预期的参数类型“[Double]”
- wakanda - Wakanda 2.7.0 Studio 未显示新的类方法
- oracle - ora-12504: tns:listener 在 windows 10 的 connect_data 中没有被赋予 service_name
- python - Pandas Dataframe 用其他列的第一个值填充列
- google-apps-script - 如何根据 Google 表格中的单元格值命名 Google Drive 文件夹