Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:56+00:00 2026-06-06T19:20:56+00:00

Background: In Drupal 7, I have created a form with CCK (aka the Field

  • 0

Background: In Drupal 7, I have created a form with CCK (aka the Field UI). I used the Field group module to create a fieldgroup, but I need it to be conditional, meaning it will only display depending on a previous answer.

Previous research: To create a conditional field, you can use hook_form_alter() to edit the #states attribute like so:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'person_info_node_form') {
    // Display 'field_maiden_name' only if married
    $form['field_maiden_name']['#states'] = array(
      'visible' => array(
        ':input[name="field_married[und]"]' => array('value' => 'Yes'),
      ),
    );
  }
}

However, there seems to be no way to use the States API for fieldgroups. One thing to note is that, while fields are stored in $form, fieldgroups are stored in $form['#groups'] as well as in $form['#fieldgroups']. I don’t know how to distinguish between these, and with this in mind, I have tried to apply a #states attribute to a fieldgroup in the same manner as above. However, it only produces server errors.

Question: Is there a way to make a fieldgroup display conditionally using the States API or some alternative approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:20:58+00:00Added an answer on June 6, 2026 at 7:20 pm

    Here’s the simplest solution I came up with. There are essentially 2 parts to this: (1.) programmatically alter the display of the form, and (2.) use the GUI to alter the display of the content.

    (1.) First, I used hook_form_alter() to programmatically create the conditional fieldset and add existing fields to it. The code is shown below.

    function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
      if ($form_id == 'FORM_ID_node_form') {
        // programmatically create a conditional fieldset
        $form['MYFIELDSET'] = array( // do NOT name the same as a 'Field group' fieldset or problems will occur
          '#type' => 'fieldset',
          '#title' => t('Conditional fieldset'),
          '#weight' => intval($form['field_PARENT']['#weight'])+1, // put this fieldset right after it's "parent" field
          '#states' => array(
            'visible' => array(
              ':input[name="field_PARENT[und]"]' => array('value' => 'Yes'), // only show if field_PARENT == 'Yes'
            ),  
          ),  
        );
    
        // add existing fields (created with the Field UI) to the
        // conditional fieldset
        $fields = array('field_MYFIELD1', 'field_MYFIELD2', 'field_MYFIELD3');
        $form = MYMODULE_addToFieldset($form, 'MYFIELDSET', $fields);
      }
    }
    
    /**
     * Adds existing fields to the specified fieldset.
     *
     * @param  array   $form Nested array of form elements that comprise the form.
     * @param  string  $fieldset The machine name of the fieldset.
     * @param  array   $fields An array of the machine names of all fields to
     *                   be included in the fieldset.
     * @return array   $form The updated form.
     */
    function MYMODULE_addToFieldSet($form, $fieldset, $fields) {
      foreach($fields as $field) {
        $form[$fieldset][$field] = $form[$field]; // copy existing field into fieldset
        unset($form[$field]); // destroy the original field or duplication will occur
      }
    
      return $form;
    }
    

    (2.) Then I used the Field group module to alter the display of the content. I did this by going to my content type and using the ‘Manage display’ tab to create a field group and add my fields to it. This way, the fields will appear to be apart of the same group on both the form and the saved content.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the background. I need to have the SAME Drupal website deployed on
Background: I have a css and a js that is used only by the
Background I have a form that PHP generates from my database. The page is
All I need to do is have a form that does this: User enters
Hi i m working on content template module in drupal,but there is problem in
Background I have a jQuery plugin I created, the plugin takes all table on
Background: What I need to accomplish is to remove any records in a collection
Background: I would like to dismiss a modalView that I have presented earlier and
Backround: Suppose I have the following Git URLs (hosted on github) http://github.com/mikl/drupal.git git://github.com/mikl/drupal.git (Git
i'm starting with wordpress (from a drupal background) and trying to figure out how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.