logo

Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Using #states in hook_form_alter

22/02/2025, by Anonymous (not verified)

Hello. I'm trying to resolve an issue regarding field dependencies in Drupal 7. From numerous questions, it has become clear that it's optimal to use #states in Drupal 7 instead of conditional fields. It turns out that another challenge with this module is the dependency problem when using the multi_step module.

Overall, I figured out how #states works. However, I only understood how it functions when creating my own form using hook_form. My task is to modify an already existing material addition form.

hook_form_ID_FORM_alter

I'm using it. I find the arrays through dsm($form);
But I can't understand how to correctly define the dependencies.

For example, in the created form, I did it like this:

<?php $form['radio_type'] = array(
    
'#type' => 'radios',
    
'#options' => array(
      
'0'   => t('Dropdown group appears'),
      '1' 
=> t('Text field inactive'),
      
'2'      => t('Checkboxes disappear'),
    ),
    
'#title' => t('Select action')
  );
  
  
$form['grupa'] = array(
    
'#type' => 'fieldset',
    
'#title' => t('Group'),
    
'#states' => array(
      
'visible' => array(
        ':input[name="radio_type"]' 
=> array('value' => '0'),
      ),
    ),
  );
?>

 

In the hook_form_alter function (currently writing in template)
I'm trying to do something like this:

<?phpfunction theme_form_zavedeniya_node_form_alter(&$form, &$form_state$form_id) {
   
dpm($form); 
   
$form['body']['und']['0']['value']['#states']  = array(
  
'invisible' => array(
    
':input[name="field_sity2"]' => array('value' => '56'),
  ),
);
}
?>

 

That is, I want to make it so that when selecting the value 56 in the field_sity2 (link to term), the body field disappears.
In general, nothing is working. I've tried various approaches, but it seems I haven't managed to get it right at all. I ask for advice.

P.S. Perhaps you could also suggest what to use for creating a multi-step material creation form with dependent fields. And in the future, to edit the material, it could be done through tabs rather than steps.