How do I display a form element only if the above checkbox is checked? I am about to create a blockForm with a display_count checkbox and two count_prefix and count_suffix textfields.

The count_prefix and count_suffix textfields shall only be shown if display_count is checked.

I have this code:

  $form['display_count'] = [
  '#type' =&gt; 'checkbox',
  '#title' =&gt; t('Display count'),
  '#description' =&gt; t('Display how many times each term is used on a node.'),
  '#default_value' =&gt; $config['display_count'],
];
$form['count_prefix'] = [
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Count prefix'),
  '#description' =&gt; t('Set a prefix to be displayed before count number e.g. (5), [5] or &lt;5&gt;'),
  '#required' =&gt; FALSE,
  '#default_value' =&gt; $config['count_prefix'],
];
$form['count_suffix'] = [
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Count suffix'),
  '#description' =&gt; t('Set a suffix to be displayed after count number e.g. (5), [5] or &lt;5&gt;'),
  '#required' =&gt; FALSE,
  '#default_value' =&gt; $config['count_suffix'],
];


I have tried to add this to count_prefix and count_suffix:

  '#states' =&gt; array(
    'visible' =&gt; array(
      ':input[name="display_count"]' =&gt; array(
        array('checked' =&gt; TRUE),
      ),
  ),
),


but it didn't worked.

I have tried to find other questions with the same but none of them worked for me.
Use like this:

$form['count_prefix'] = [
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Count prefix'),
  '#description' =&gt; t('Set a prefix to be displayed before count number e.g. (5), [5] or &lt;5&gt;'),
  '#required' =&gt; FALSE,
  '#default_value' =&gt; $config['count_prefix'],
  '#states' =&gt; array(
    'visible' =&gt; array(
      ':input[name="display_count"]' =&gt; array('checked' =&gt; TRUE),
    ),
  ),
];
$form['count_suffix'] = [
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Count suffix'),
  '#description' =&gt; t('Set a suffix to be displayed after count number e.g. (5), [5] or &lt;5&gt;'),
  '#required' =&gt; FALSE,
  '#default_value' =&gt; $config['count_suffix'],
  '#states' =&gt; array(
    'visible' =&gt; array(
      ':input[name="display_count"]' =&gt; array('checked' =&gt; TRUE),
    ),
  ),
];