Drupal:如何在 hook_field_widget_form 中使用字段集

发布于 2024-11-01 23:05:55 字数 209 浏览 1 评论 0原文

所以我有我的 hook_field_schema 定义列,并且 hook_field_widget_form 已设置并正确保存所有列值。

但是,一旦我将两个字段放入字段集中,这些值就永远不会保存或更新。我尝试过设置#tree =>到处都是 FALSE,这也不起作用。

我缺少什么?只是不支持吗?我应该使用 form_alter 钩子或其他东西将它们移动到字段集中吗?

So I have my hook_field_schema defining columns, and hook_field_widget_form is set up and saving all of the column values correctly.

But as soon as I put two of the fields inside of a fieldset, those values never save or get updated. I've tried setting #tree => FALSE all over the place and that isn't working either.

What am I missing? Is it just unsupported? Should I be using a form_alter hook or something to move them into a fieldset?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淑女气质 2024-11-08 23:05:55

我有同样的问题,找不到解决方案。在尝试了很多事情之后,结果证明这是一件简单到不合逻辑的事情。嗯,在 Drupal 新手的眼中。

起初,我有这样的东西(精简版本):

$element['mymodulefieldset'] = array(
  '#title' => 'Fieldset title',
  '#type' => 'fieldset',
);

并将字段添加到字段集中:

$element['mymodulefieldset']['fieldname'] = array(
  '#title' => "Field title",
  '#type' => 'textfield',
  '#default_value' => '',
);

在尝试了很多不同的场景之后,我发现接下来的代码行确实(有点)有效。我没有插入字段集,而是将元素转换为如下所示的字段集:

$element += array(
  '#type' => 'fieldset',
  '#tree' => true
);

然后我向元素添加了字段:

$element['fieldname'] = array(
  '#title' => "Field title",
  '#type' => 'textfield',
  '#default_value' => '',
);

注意:#title 和 #weight 等一些变量由“主页 » 管理 » 结构 » 内容类型 » [您的内容”控制TYPE]”,其他的(如#collapsible和#collapsed)可以在这里定义。

希望这对您有帮助!

I had the same problem and couldn't find a solution. After trying many things, it turned out to be something as simple as illogical. Well, in the eyes of a Drupal newbie.

At first I had something like this (stripped down version):

$element['mymodulefieldset'] = array(
  '#title' => 'Fieldset title',
  '#type' => 'fieldset',
);

and added fields to the fieldset:

$element['mymodulefieldset']['fieldname'] = array(
  '#title' => "Field title",
  '#type' => 'textfield',
  '#default_value' => '',
);

After trying lots of different scenarios I found out the next lines of code did (sort of) work. Instead of inserting a fieldset, I turned the element into a fieldset like this:

$element += array(
  '#type' => 'fieldset',
  '#tree' => true
);

Then I added fields to the element:

$element['fieldname'] = array(
  '#title' => "Field title",
  '#type' => 'textfield',
  '#default_value' => '',
);

NB: some variables like #title and #weight are controlled by "Home » Administration » Structure » Content types » [YOUR CONTENT TYPE]", others (like #collapsible and #collapsed) can be defined here.

Hope this helps you out!

桃气十足 2024-11-08 23:05:55

我知道这是一个老问题,但是这个问题有一个解决方案,解释在本文中,#process 部分用于正确保存字段。

编辑:正如@alexkb的评论所解释的那样,文章的作者更新了他的自定义字段示例 并删除了#process hack。如需更好的解决方案,请使用 GitHub 代码。

I know it is an old question, but there is a solution for this problem, explained in this artice, the #process part is what is used to save the fields properly.

EDIT: As the comment of @alexkb explains, the author of the article has updated his custom field sample and has removed the #process hack. For a better solution, use the GitHub code.

盗梦空间 2024-11-08 23:05:55

查看物理模块。基本上是:

$element['#type'] = 'fieldset'

您可以使用以下方式定义子字段:

$element['subfield'] = array (...);

Look at the physical module. Basically do:

$element['#type'] = 'fieldset'

You can define the sub fields with:

$element['subfield'] = array (...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文