I'm trying to build a block configuration form. Here's my blockForm code:

  public function blockForm($form, FormStateInterface $form_state) {
    $form['resolve_ip_addresses'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Resolve IP Addresses'),
      '#default_value' => $this->configuration['resolve_ip_addresses'],
      '#description' => $this->t('Resolve IP addresses to return host names for log file entries.'),

    $form['log_file_path'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Log File Path'),
      '#default_value' => $this->configuration['log_file_path'],
      '#description' => $this->t('The full file system path to the log file to be parsed.'),

    $form['log_file_format'] = [
      '#type' => 'radios',
      '#title' => $this->t('Log Format'),
      '#default_value' => $this->configuration['log_file_format'],
      '#options' => [
        0 => $this->t('Common Log Format (Apache)'),
        1 => $this->t('Combined Log Format (Nginx)'),
        2 => $this->t('Other'),
      '#attributes' => [
        'name' => 'field_log_file_format',
    $form['custom_log_format'] = [
      '#type' => 'textfield',
      '#size' => '60',
      '#placeholder' => $this->t('Enter log format'),
      '#default_value' => $this->configuration['custom_log_format'],
      '#attributes' => [
        'id' => 'custom-log-format',
      '#states' => [
        'visible' => [
          ':input[name="field_log_file_format"]' => ['value' => 2],

    return $form;

The form seems to be displayed properly. I'm having an issue getting the value for the "log_file_format" field. Here's my blockSubmit code:

  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['resolve_ip_addresses'] = $form_state->getValue('resolve_ip_addresses');
    $this->configuration['log_file_path'] = $form_state->getValue('log_file_path');
    $this->configuration['log_file_format'] = $form_state->getValue('log_file_format');
    $this->configuration['custom_log_format'] = $form_state->getValue('custom_log_format');

The problem is that the value returned from $form_state->getValue('log_file_format') is always 0 (zero), no matter what value I select on the form and save the block. All of the other values are correct. Interestingly, the "custom_log_format" field correctly switches visibility on the form when the "Other" option (value 2) is selected. What am I doing wrong here?

  • I think it's sure custom name attribute 'name' => 'field_log_file_format', messing it up. Try removing that to see if it then works Feb 28, 2023 at 13:55
  • That does indeed seem to be the problem. Removing that attribute breaks the visibility state for the "custom_log_format" field, though. How can I toggle the visibility so that the field is only visible if "Other" is selected? Changing ":input[name="field_log_file_format"]" to ":input[name="log_file_format"]" isn't working. Feb 28, 2023 at 15:12
  • Another interesting point: my original code was based on samples found on Drupal's own documentation: drupal.org/docs/drupal-apis/form-api/conditional-form-fields Feb 28, 2023 at 17:00
  • Remove 'name' => 'field_log_file_format', and change ':input[name="field_log_file_format"]' to ':input[name="log_file_format"]' should work. Because when you remove the attributes name, Drupal will add name='log_file_format' to that radios field. You check the #states of 'custom_log_format' on that radios field ('log_file_format' by default) Mar 1, 2023 at 1:53
  • In case you modify those both not help(in saving and switch visibility), try replace ['value' => 2] to ['value' => '2']. The numeric and string may the culprit. Mar 1, 2023 at 2:05

1 Answer 1


The fix suggested by @unusedspoon took care of the radio element value problem. The form element value was properly set after removing the 'name' => 'field_log_file_format' attribute from the 'log_file_format' form element - but that broke the visibility toggle for the 'custom_log_format'. The fix for that was to change ':input[name="field_log_file_format"]' to ':input[name="settings[log_file_format]"]'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.