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

  1. Install and enable the Charts and Chart.js Integration modules on an environment running PHP 8.0+.
  2. Create a View utilizing a Pie/Doughnut or Scatter chart display type.
  3. Configure the data fields so that the dataset yields single numeric string values or single-item arrays instead of standard multi-coordinate mappings.
  4. Load the page containing the chart and inspect the watchdog logs or system error outputs to observe the Warning: Undefined array key 1 error.

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 $data payload 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

Command icon 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

ooa33 created an issue. See original summary.

ooa33’s picture

StatusFileSize
new1.69 KB

andileco’s picture

Status: Needs review » Fixed

Patch is in. Thanks for your contribution!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.