Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm doing what I think is a pretty simple form alter to utilize D7's form_state, so I can conditionally show a field group, its like this:
function antonelli_form_endoscopy_record_node_form_alter(&$form, &$form_state) {
// fieldset colonoscopy
$form['#fieldgroups']['group_colonoscopy'] = array(
'#states' => array(
'visible' => array(
'#edit-field-procedure-und' => array('value' => t('colonoscopy')),
),
),
);
// fieldset gastroscopy
$form['#fieldgroups']['group_gastroscopy'] = array(
'#states' => array(
'visible' => array(
'#edit-field-procedure-und' => array('value' => t('gastroscopy')),
),
),
);
// print kpr($form);
}
The problem is the fieldgroups disappear entirely from the form, not just hidden, they are not printed at all, and I don't really understand why not because I have done this before with another form.
Any pointers much appreciated.
Comment | File | Size | Author |
---|---|---|---|
#48 | 1053174-48-field_group-D8-form-states.patch | 1.92 KB | ElusiveMind |
#43 | 1053174-40-field_group-D8-form-states.patch | 1.76 KB | swentel |
#40 | 1053174-40-field_group-D8-form-states.patch | 1.76 KB | FatherShawn |
#31 | 1053174-31-field_group-D8-form-states.patch | 2.47 KB | eric.chenchao |
#5 | fieldgroup-issue-1.png | 38.02 KB | Jeff Burnz |
Comments
Comment #1
Jeff Burnz CreditAttribution: Jeff Burnz commentedI should mention that when I run this code I get a bunch of php notices all pertaining to field_group_field_group_pre_render(), such as:
Comment #2
Stalski CreditAttribution: Stalski commentedI just tried it and it works here. Cool trick you learned me.
But about the problem you have. The notices are not good at all and indicate that the group that was given to the pre_render does not exist.
Difficult to say why that would occur. Did you do something custom for fieldgroups?
Comment #3
Stalski CreditAttribution: Stalski commentedah I think you need to do like i did:
As the fieldgroup will be gone ofcourse with everything in it. Maybe that is what your form alter does and why you see the missing group given. Does that fix your problem?
- Edit
Comment #4
Jeff Burnz CreditAttribution: Jeff Burnz commentedActually using [#states] gives a fatal error :)
The only thing different I am doing is that both form elements are nested inside a vertical tab (done with Fieldgroup module), but I wouldn't think that makes a difference?
I tried just changing the #title of the fieldset inside the form alter and even with just that the fieldset does not print, such as:
So either I am doing something wrong with the form alter or something is going wrong inside fieldgroup module?
Comment #5
Jeff Burnz CreditAttribution: Jeff Burnz commentedJust adding a small picture to show what I am trying to achieve here - you will see there is a select list field with two options, when one option is chosen the related fieldset should show and the other one remain "hidden". However, as I mentioned before whenever I run the form alter on either of those fieldgroup fieldsets they don't print in the form at all, this is the bug I think? Anyway, hope the image helps clarify what I am doing in case its not clear.
Comment #6
Stalski CreditAttribution: Stalski commentedYeah, i know what the problem is. I will send you the solution in a minute. #fieldgroups contains the group object, not the form element.
Comment #7
Stalski CreditAttribution: Stalski commentedFor module named custom:
You will find the elements in the array there.
So you were doing nothing wrong, just the wrong hook. As fieldgroup can only work after the alter, it provides its own hooks to alter things.
You will need to flush cache and debugging like with dsm will only appear the second page load.
Comment #8
Stalski CreditAttribution: Stalski commentedCan you close this if the problem is solved.
Comment #9
Jeff Burnz CreditAttribution: Jeff Burnz commentedOK I am using the new hook and tried with the following code but the assignment operator gives me a fatal error (Fatal error: Unsupported operand types), which I assume is because something in the array is bad, but I don't understand this well enough to figure it out.
If I just use a strait = it does get added (the #states) but of course it all fails because everything else gets wiped out, oddly though the fieldset keeps printing as they seem to be in two places - both in #groups and #fieldgroups
I also get this notice:
Line 41 is:
$element['#fieldgroups']['group_gastroscopy']
Comment #10
Stalski CreditAttribution: Stalski commentedOk i will give you working code. #fieldgroups is not an element! Elements in drupal are never with # , that are properties.
Comment #11
Stalski CreditAttribution: Stalski commentedSorry had a computer crash and appearently forgot about this one not being posted.
Here is what i came up that works for a group called group_content in a vertical tab or horizontal tab.
Comment #12
Jeff Burnz CreditAttribution: Jeff Burnz commentedI'm really sorry but I still get the unsupported operand error for the assignment operator.
This is the code I used, based on the above, my module is called "appx", I have a fieldset called 'group_colonoscopy' which is inside a tab (I've switched to horizontal tabs now as this makes more sense for the layout of the form).
Comment #13
Stalski CreditAttribution: Stalski commentedAnd you are really sure that "$element['group_colonoscopy']" exists??
What does dsm tell you on the $element or $element['group_colonoscopy'] ? That is the devel tool to log messages to the screen. When doing things like that, you must use it, there is no other way (that is better).
I think you just tried that without checking if it exists, no? As i said before, you can try
but in this case, i am pretty sure the element is not there. It will be nested somewhere deeper. In your case, it will be a little trial and error.
Comment #14
Jeff Burnz CreditAttribution: Jeff Burnz commentedOK, part of my problem is that what Devel is showing me in Krumo is not accurate to the structure of the form, instead I just went off the manage fields display and guessed the structure, so I now have the states being added in the right place etc.
However I now have an issue with the JS settings not being added correctly - the normal fieldset ID's are not printing and not being added to the JS settings, such as...
This...
...should be this...
Even hacking the ID attribute into field_group_field_group_pre_render() such as this did not work...
Now, it does work with fields within the fieldset, so this is what I will do if we can't solve this last issue, I'll pull the fields out of the fieldset and delete that group, which is not ideal but at least I now have a way of making this work!
Thanks so much for your patience and code snippets, really appreciate all of it :)
Comment #15
Stalski CreditAttribution: Stalski commentedYeah, i had that error too since it was a quick preview.
I think you best add a few checks more ;), like isset($element['group_content'])
Comment #16
rv0 CreditAttribution: rv0 commentedDoes not seem to work with horizontal tabs nested in vertical tabs
dpm($element) in hook_field_group_build_pre_render_alter never shows the horizontal tabs.
changed execution order in hook_module_implements_alter but did not work either.
is there some other mechanism required for horizontal tabs?
Comment #17
griz CreditAttribution: griz commentedI think this whole thread is very anal.
Comment #18
Jeff Burnz CreditAttribution: Jeff Burnz commentedFor those not aware I believe griz is making a joke here, due to the subject nature of the site I was working on... for recording endoscopic records...
Comment #19
Martijn Houtman CreditAttribution: Martijn Houtman commentedThanks, I was looking for this hook!
Comment #20
clairedesbois@gmail.comHello,
The #id have been removed. If you want use #states on your field group, you must add it:
Comment #21
WorldFallz CreditAttribution: WorldFallz commentedJust a confirmation.... #20 works for me.
Comment #22
anouThanks to Calystod! That's the issue solution.
Comment #23
jimmyko CreditAttribution: jimmyko as a volunteer commented#20 cannot work for me. I am using field_group-7.x-1.5
Anyone has idea?
===
update:
pre_render did the trick. But please note that
dpm()
is not working in the pre_render hook. You have to usekpr()
orvar_dump()
for your debug purpose.Comment #24
wildfeed CreditAttribution: wildfeed as a volunteer commentedkpr($element['my_group'])
helped me find out the value for
'#id' => 'my_group',
In my case it was
'#id' => 'edit-profile-main',
that turned the trick.
Comment #25
mibfire CreditAttribution: mibfire commentedI have a radio box and i would like to hide a group(section) but this doesnt work. Here is my code:
I added id to group, field_group is 1.5. There is no php or js error.
The solution:
I had to set id into field group settings in UI and add wrapper around the group because state.js finds a wrapper(.form-item or .form-submit or .form-wrapper) to hide. Take a look at code snippet from state.js:
And the php code is:
Comment #26
joewhitsittI am unable to get #states to work with my nested field_group. #20 works for "top-level" fieldsets.
My structure is such:
The structure $element['group_my_vtab']['group_my_fieldset'] doesn't exist. Instead the structure of $element['group_my_vtab'] is this:
I can't get to the group_my_fieldset object within hook_field_group_build_pre_render_alter() unless i target $element['#groups'] or $element['#fieldgroups']. I tried these as well, but wasn't successful.
Comment #27
miiimoooSolution in #25 worked for me too (in D8). Thanks @mibfire
I did this (which is identical with #25 for two small changes/additions (note this is a fieldgroup inside another fieldgroup)
Comment #29
handkerchiefComment #30
handkerchiefHow is it going on? Is the workaround with the custom wrapper in MYMODULE_field_group_build_pre_render_alter() the final solution?
Comment #31
eric.chenchao CreditAttribution: eric.chenchao commentedFor Drupal 8, 'js-form-wrapper' class is required for Form states.js. If we could add this class into the form HTML, that avoids to using an extra wrapper.
Or we could apply the patch attached to add those HTML class to the template and the code will be:
The patch should work in the both 8.x-1.x and 8.x-3.x branches.
Comment #32
handkerchief@eric.chenchao it works like a charm! So it would be great the patch would now be added to the module.
Comment #33
GaëlGPatch #31 works. Thanks! :)
Comment #34
jimmyko CreditAttribution: jimmyko as a volunteer commentedWill the fix back port to 7.x module?
Comment #35
DuneBLI don't know why, but MYMODULE_field_group_build_pre_render_alter is not called...
I have tried also with MYTHEME_field_group_build_pre_render_alter without success.
I have even added a dpm and a drupal_set_message in the module itself: field_group_field_group_build_pre_render_alter
...without luck!!!
I have tested with fieldset, detail, acordion,... all the possibilities
Drupal 8.5
field_group 8.x-3.x
The fieldset is in a contact form.
Any hint/help?
Comment #36
DuneBLregarding #35, I could hide/unhide a fieldset (based on the value of a select list) by replacing the fieldset group made in the UI by a fieldset group created in form alter.
I had to move the 2 fields (created in the UI) that have to be in the fieldset inside this newly created fieldset
...This is working without patch...
Conclusion:
- '#STATES' with fieldset created in UI =>I assume you should use #31
- '#STATES' with fieldset created in FORM_alter =>Works as expected
In all cases, as stated in #35, MYMODULE_field_group_build_pre_render_alter is never called
Comment #37
nathandentzauI used
hook_field_group_form_process_build_alter()
to get #31 to work.Comment #38
larowlanout of scope changes?
whitespace issue here.
whitespace issues
Comment #39
sagesolutions CreditAttribution: sagesolutions commentedI also have the same issue. I created a module and implemented the hook_field_group_build_pre_render_alter() function, but it was never called.
Comment #40
FatherShawnHere's a re-roll of #31 against 3.0-RC1 release.
Comment #41
AnybodyChanging the branch to the future one.
Comment #42
texas-bronius CreditAttribution: texas-bronius at Ocelot commentedRerolled #40 patch and states works for me to be able to do the hooks like in #31, thank y'all!
Note that #31 is about field_group on display, whereas I needed it on form alter using
hook_field_group_form_process_build_alter()
which works.Comment #43
swentel CreditAttribution: swentel at eps & kaas for Dropsolid commentedre-uploading patch for the bot
Comment #44
swentel CreditAttribution: swentel at eps & kaas for Dropsolid commentedHmm, I'm a bit on the fence here to add those classes in the default templates as they are not needed by default. The HTML element suddenly looks well, a bit silly. Since they are templates, you can also just override the template if you really need them.
This issue is also extremely old, so closing.
Comment #45
guistoll CreditAttribution: guistoll commentedHere's how I'm adding #states on a field group:
I have a checkbox called field_custom_content and a group called group_custom_content. The group will only be displayed when field_custom_content checkbox is true/checked.
These fields are in a paragraph inside a content type.
Comment #46
SKAUGHTfield_group.api.php --> hook_field_group_form_process() worked for me
D8 field group 3.1
Comment #47
wstocker CreditAttribution: wstocker at Mediacurrent commentedPatch #43 is failing with the composer update to drupal/field_group (3.2.0)
on diff --git a/templates/horizontal-tabs.html.twig b/templates/horizontal-tabs.html.twig
Comment #48
ElusiveMind CreditAttribution: ElusiveMind as a volunteer commentedUpdated patch file to work with field_group 3.2.0
Comment #49
lind101 CreditAttribution: lind101 commentedFor those landing here looking for a way to add #states to field_groups in a more generic way via traditional form_alter_hooks, the below helper hook works well.
Comment #50
dmezquiaThe hook
hook_field_group_form_process
works ok for me too.