4

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?

Karl
  • 773
  • 2
  • 9
  • 22

3 Answers3

5

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!

lmeurs
  • 16,111
  • 4
  • 27
  • 30
  • I'm voting this answer up because it will definitely be helpful to others searching for fieldset information, but this wasn't my issue. I had some form fields inside of a fieldset, and some outside of one, and the ones inside weren't saving. – Karl May 13 '11 at 19:53
4

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.

o15a3d4l11s2
  • 3,969
  • 3
  • 29
  • 40
  • 2
    The article you've linked to talks about #process but you'll notice the author has updated his [sample code on github](https://github.com/tarmstrong/poutine_maker/blob/master/poutine_maker.module) to use hook_field_presave() - which is a much better approach than the #process hack. I'm voting your answer up though, as it led me to a solution that I thought I wouldn't be able to solve, thanks! – alexkb Feb 06 '15 at 03:33
  • i found by myself the solution using hook_field_presave but be aware that with that solution the code ` '#default_value' => isset($item['value']) ? $item['value'] : ''` won't work in case of fields attached to entities like paragraph – Nicoschi Nov 04 '19 at 17:50
1

Look at the physical module. Basically do:

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

You can define the sub fields with:

$element['subfield'] = array (...);
Berend de Boer
  • 1,953
  • 20
  • 15