Fieldgroup #states not working

Created on 8 March 2015, almost 10 years ago
Updated 10 April 2023, over 1 year ago

Hi,

I'm trying to build a complex conditional form with fieldgroup multipage but i'm unable to show groups of fields after an option (from a list of checkboxes) has been ticked.

Is a custom module that implement an entity and fields.

This is the group:


  $field_group = new stdClass();
  $field_group->disabled = FALSE; 
  $field_group->api_version = 1;
  $field_group->identifier = 'group_raw_materials|organisation|organisation|form';
  $field_group->group_name = 'group_raw_materials';
  $field_group->entity_type = 'organisation';
  $field_group->bundle = 'organisation';
  $field_group->mode = 'form';
  $field_group->parent_name = 'group_specific';
  $field_group->data = array(
    'label' => 'Raw Materials',
    'weight' => '0',
    'children' => array(
      0 => 'hub_rawmaterials_resins',
      1 => 'hub_rawmaterials_fibres',
      2 => 'hub_rawmaterials_consumables',
      3 => 'hub_rawmaterials_core',
      ),
    'format_type' => 'div',
    'format_settings' => array(
      'label' => 'Raw Materials',
      'instance_settings' => array(
        'required_fields' => 1,
        'id' => 'group-raw-materials',
        'classes' => 'collapsidiv',
        'description' => '',
        'show_label' => '1',
        'label_element' => 'h3',
        'effect' => 'none',
        'speed' => 'fast',
      ),
      'formatter' => 'collapsed',
    ),
   );
  $info['group_raw_materials|organisation|organisation|form'] = $field_group;

This is the function I'm trying to use:

function organisationform_field_group_build_pre_render_alter(&$element) {
	$element['group_wrapper']['group_specific']['group_raw_materials'] += array(
      '#states' => array(
		'visible' => array(
		 '#edit-hub-position-activities-und-raw-materials' => array('checked' => TRUE),
		),
            ),
	);
}

and this is what I get form my print_r function: (print_r($element['group_wrapper']['group_specific']['group_raw_materials'])

 [#states] => Array
        (
            [visible] => Array
                (
                    [#edit-hub-position-activities-und-raw-materials] => Array
                        (
                            [checked] => 1
                        )

                )

        )

So everything seems correct but the group is still showing.
I've also tried to downgrade fieldgroup with no better results.

I'm quite new in the module development and I'd appreciate any help.

πŸ’¬ Support request
Status

Active

Version

1.4

Component

Miscellaneous

Created by

πŸ‡¬πŸ‡§United Kingdom matteo.borgognoni

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΈπŸ‡ͺSweden twod Sweden

    You need to do something like this:

    /**
     * Implements hook_form_alter().
     */
    function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
      $form['#pre_render'][] = 'MYMODULE_form_pre_render';
    }
    
    function MYMODULE_form_pre_render($element) {
        $group = 'THE_GROUP_NAME';
        $element[$group]['#states'] = THE_STATES_ARRAY;
        // #states requires an element id, which groups don't have by default.
        $element[$group]['#id'] = $group;
        drupal_process_states($element[$group]);
      }
      return $element;
    }
    
    
Production build 0.71.5 2024