Warning: Undefined array key 1 in Drupal\charts_chartjs\Plugin\chart\Library\Chartjs->populateDatasets() (line 561 of modules/contrib/charts/modules/charts_chartjs/src/Plugin/chart/Library/Chartjs.php).
Steps to reproduce
Proposed resolution
Rem
Problem/Motivation
When running the Charts module with the Chart.js integration sub-module on PHP 8.0 or higher, a warning notice is triggered: Warning: Undefined array key 1 in Drupal\charts_chartjs\Plugin\chart\Library\Chartjs->populateDatasets() (line 561 of modules/contrib/charts/modules/charts_chartjs/src/Plugin/chart/Library/Chartjs.php).
This occurs inside the populateDatasets() loop when handling Pie, Doughnut, Scatter, or Bubble chart styles. The code assumes that the data element array always contains multiple elements or coordinate keys (e.g., index 1 for y-coordinates or label matches). When the data passed from a Views dataset or custom API implementation contains a flat single-value item or an array lacking consecutive numeric indices, PHP 8+ explicitly catches and reports this uninitialized key access instead of failing silently.
Steps to reproduce
- Install and enable the Charts and Chart.js Integration modules on an environment running PHP 8.0+.
- Create a View utilizing a Pie/Doughnut or Scatter chart display type.
- Configure the data fields so that the dataset yields single numeric string values or single-item arrays instead of standard multi-coordinate mappings.
- Load the page containing the chart and inspect the watchdog logs or system error outputs to observe the
Warning: Undefined array key 1error.
Proposed resolution
Implement a defensive safety check inside Chartjs->populateDatasets() before evaluating numeric indexes on variables that could potentially be scalars or flat configurations.
- Add
is_array()and null coalescing checks (??) to verify that indices like$data[1]or$data[2]exist before they are read or manipulated. - Ensure that if
$data[1]is missing, the code gracefully falls back to using the entire$datapayload or defaults to a safe base structure.
Remaining tasks
- Create and review a patch / merge request with the defensive null-coalescing validation.
- Test the fix against multiple chart variants (Pie, Scatter, Bubble) to confirm the warning is completely suppressed.
- Commit the fix to the branch.
User interface changes
None. This is a backend data validation patch.
API changes
None.
Data model changes
None.
aining tasks
User interface changes
API changes
Data model changes
Issue fork charts-3614833
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
ooa33 commentedComment #5
andileco commentedPatch is in. Thanks for your contribution!