- Issue created by @dan_metille
The `LicenseTypeBase::submitConfigurationForm()` method triggers the following PHP warning when saving configurations that lack all expected properties:
Warning: Trying to access array offset on value of type null in Drupal\commerce_license\Plugin\Commerce\LicenseType\LicenseTypeBase->submitConfigurationForm() (line 81 of modules/contrib/commerce_license/src/Plugin/Commerce/LicenseType/LicenseTypeBase.php).
This happens because the method assumes that for every property defined in `defaultConfiguration()`, there is a corresponding form value. However, if a form element is not set or is missing, this assumption results in an error.
Modify the `submitConfigurationForm()` method to safely handle missing configuration values by providing fallbacks to the current configuration or default values.
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValue($form['#parents']);
$property_names = array_keys($this->defaultConfiguration());
foreach ($property_names as $property) {
// Use a fallback to the current configuration or default value if the form value is not set.
$this->configuration[$property] = $values[$property] ?? $this->configuration[$property] ?? $this->defaultConfiguration()[$property];
}
}
1. Install the Commerce License module.
2. Create or update a license configuration where certain expected form values are missing.
3. Save the configuration.
4. Observe the PHP warning in the logs or on-screen.
No warning is triggered. Missing values are gracefully handled by falling back to the current configuration or default values.
A PHP warning occurs when accessing undefined array offsets.
A patch file is attached to this issue.
Active
3.0
Code