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?