Score:0

Block Configuration Form Returning Unexpected Value

mx flag

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?

unusedspoon avatar
aq flag
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
Scott Hollenbeck avatar
mx flag
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.
Scott Hollenbeck avatar
mx flag
Another interesting point: my original code was based on samples found on Drupal's own documentation: https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields
Trà Dương avatar
sa flag
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)
Trà Dương avatar
sa flag
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.
Scott Hollenbeck avatar
mx flag
Thanks for the suggestions. I've tried both changes. Getting rid of the "name" attribute fixed the problem with the value of the log_file_format field, but the custom_log_format field is still visible. Changing ['value' => 2] to ['value' => '2'] made no difference.
Scott Hollenbeck avatar
mx flag
Got it: I needed to change "':input[name="field_log_file_format"]'" to "':input[name="settings[log_file_format]"]'" to get the visibility toggle working. I don't know why, but inspecting the form elements as they were displayed in my browser showed that the name attribute of the radio elements was "settings[log_file_format]".
Score:0
mx flag

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]"]'.

I sit in a Tesla and translated this thread with Ai:

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.