0

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

I have tried to add this to count_prefix and count_suffix:

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

but it didn't worked.

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

0

Use like this:

$form['count_prefix'] = [
  '#type' => 'textfield',
  '#title' => t('Count prefix'),
  '#description' => t('Set a prefix to be displayed before count number e.g. (5), [5] or <5>'),
  '#required' => FALSE,
  '#default_value' => $config['count_prefix'],
  '#states' => array(
    'visible' => array(
      ':input[name="display_count"]' => array('checked' => TRUE),
    ),
  ),
];
$form['count_suffix'] = [
  '#type' => 'textfield',
  '#title' => t('Count suffix'),
  '#description' => t('Set a suffix to be displayed after count number e.g. (5), [5] or <5>'),
  '#required' => FALSE,
  '#default_value' => $config['count_suffix'],
  '#states' => array(
    'visible' => array(
      ':input[name="display_count"]' => array('checked' => TRUE),
    ),
  ),
];
  • Hi, I have tried that but it doesn't work. – user72672 Sep 15 '17 at 16:14
  • Thank you now it works i used this ':input[name="settings[display_count]"]' => ['checked' => TRUE], and the new array syntax now it works :) – user72672 Sep 15 '17 at 16:40
  • Why do i need to use settings[display_count] instead of just display_count? – user72672 Sep 15 '17 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy