Fix Warning in submitConfigurationForm() for Missing Configuration Values

Created on 21 January 2025, 1 day ago

Problem/Motivation:

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.

Proposed Resolution:

Modify the `submitConfigurationForm()` method to safely handle missing configuration values by providing fallbacks to the current configuration or default values.

Updated Code:

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];
  }
}

Steps to Reproduce:

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.

Expected Behavior:

No warning is triggered. Missing values are gracefully handled by falling back to the current configuration or default values.

Actual Behavior:

A PHP warning occurs when accessing undefined array offsets.

Patch:

A patch file is attached to this issue.

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇰🇬Kyrgyzstan dan_metille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024