Creating a Configuration Form
A complete, schema-backed Drupal configuration form with validation and dependency injection.
Configuration forms should store deployable settings, validate meaningful constraints, and declare schema so Drupal can type, translate, and inspect the values safely.
Extend ConfigFormBase
Return the editable configuration names, build defaults from configuration, and save through the editable object. Use dependency injection for validation that requires services.
Validate relationships
Element constraints such as required and maxlength are only the first layer. Validate relationships between values and normalize input before saving.
Ship configuration schema
Schema describes the data types and labels under your configuration key. Without it, translations and automated configuration validation lose important context.
Working example
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config('developer_journal.settings')
->set('featured_count', (int) $form_state->getValue('featured_count'))
->save();
parent::submitForm($form, $form_state);
}Key Takeaways
- Use configuration for deployable settings, not runtime state.
- Validate business relationships server-side.
- Always provide configuration schema.