Score:2

Disable a field if another field is disabled

lb flag

I need to disable field A if field B is disabled too. Actually, my current field A has the following #states (altered using hook_alter_form) to disable it if field B or field C are empty:

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, $form_state, $form_id) {
 
  $form['field_a']['#states'] = [
    'disabled' => [
      [':input[name="field_b[0][value][date]"]' => ['empty' => TRUE]],
      'or',
      [':input[name="field_c[0][value][date]"]' => ['empty' => TRUE]],
    ],
  ];
}

And it works perfectly, but I need to add the condition mentioned before. I found this in the official documentation: https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields and I tried to use some of them like 'readonly' or 'readwrite' but this didn't work. Can I evaluate properties like '#disabled' from here? something similar to this:

function my_module_form_alter(&$form, $form_state, $form_id) {

  $form['field_a']['#states'] = [
    'disabled' => [
      [':input[name="field_b[0][value][date]"]' => ['empty' => TRUE]],
      'or',
      [':input[name="field_c[0][value][date]"]' => ['empty' => TRUE]],
      'or',
      [':input[name="field_b"]' => ['#disabled' => TRUE]],
    ],
  ];
}
in flag
You might want to check out the [Conditional Fields module](https://www.drupal.org/project/conditional_fields). It might cover your needs.
Score:0
cn flag

You can't do this with states here is what the docs says

The following states may be used when checking values of other fields:

empty
filled
checked
unchecked
expanded
collapsed
value

So, for this particular feature you will need to use some javascript or jquery.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.