Problem/Motivation

Upgraded an inherited site from D9 to D10. Once upgrade complete, no charts display, console shows an error:

Cannot read properties of undefined (reading 'google')
    at Object.attach (charts_google.js?v=1.x:18:51)
    at drupal.js?v=10.1.5:166:24
    at Array.forEach (<anonymous>)
    at Drupal.attachBehaviors (drupal.js?v=10.1.5:162:34)
    at drupal.init.js?v=10.1.5:32:12
    at HTMLDocument.listener (drupal.init.js?v=10.1.5:20:7)

Steps to reproduce

Upgrade:

  • Drupal 9 -> Drupal 10.1.5
  • Charts 3.5 -> Charts 5.0.8
  • Install Google Charts Library [following the readme in \charts\modules\charts_google - composer used]

IN charts_google.js
The error I have is down to const globalOptions = drupalSettings.charts.google.global_options;

  /**
   * Behavior to initialize Google Charts.
   *
   * @type {{attach: Drupal.behaviors.chartsGooglecharts.attach}}
   */
  Drupal.behaviors.chartsGooglecharts = {
    attach: function (context) {
      const globalOptions = drupalSettings.charts.google.global_options;
      let useMaterialDesign = globalOptions.useMaterialDesign;
      let chartType = globalOptions.chartType;

If I comment this out and hard code the variables, I get the charts displaying.

  /**
   * Behavior to initialize Google Charts.
   *
   * @type {{attach: Drupal.behaviors.chartsGooglecharts.attach}}
   */
  Drupal.behaviors.chartsGooglecharts = {
    attach: function (context) {
      //const globalOptions = drupalSettings.charts.google.global_options;
      let useMaterialDesign = false;
      let chartType = 'line';

In /admin/config/content/charts
I can change configuration option - the Charting library is set to Google.
What am I missing?

Thank you.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ice70 created an issue. See original summary.

andileco’s picture

Hi @ice70 - I need to create an environment with 3.5 - I only minimally support that version, as it has been 3 years since 5.0.x-alpha1 was released (and there was an 8.x-4.x before that). So until I have a chance to try to replicate, here's my advice:

1 - make sure that update.php has been run
2 - re-save the charts configuration
3 - re-save charts format settings and their respective views that aren't working (this should be happening during the update.php, but it doesn't hurt to try again; if you require the dev-5.0.x, it has an additional views re-save update)

If none of the following work, please look both in the browser console and the database log for errors and/or warnings.

Thanks!

andileco’s picture

Oh, information about your views set-up is also helpful (what type of field provides value, etc.).

andileco’s picture

Oh, information about your views set-up is also helpful (what type of field provides value, etc.).

ice70’s picture

Hi @andileco,
thank you for your reply :)

> 1 - make sure that update.php has been run
Made the update via composer followed by drush updatedb
I think ALL the views with charts went through the update process.
I tried 5.0.x-dev, which also ran through the update process.
There are no more updates available at update.php

> 2 - re-save the charts configuration
on /admin/config/content/charts I tried the following:
- changing the charting library from google to highcharts and save, then back to google and save.
- in google settings, enable material design and save, then disable material design and save - this also generated a warning the missing key outlined in https://www.drupal.org/project/charts/issues/3390787 hence the 5.0.x-dev tryout.
Undefined array key "use_material_design" in Drupal\charts_google\Plugin\chart\Library\Google->buildConfigurationForm() (line 113 of \web\modules\contrib\charts\modules\charts_google\src\Plugin\chart\Library\Google.php)

> 3 - re-save charts format settings and their respective views that aren't working
I have little experience with how charts / views work together, so I'll try and keep it as simple as possible hopefully without missing important information.

View is made up of two displays:

Display 1: Attachment to the view block [Display 2]:
Format:
- chart - Google / Bar with Taxonomy term: short description label. Stacking enabled.
- Show: fields, with Provide default field wrapper elements enabled.

Fields:
- content: combination of a
-- content field,
-- taxonomy term [duplicated, one with default and the other with short description]
-- COUNT: content: id
All fields have aggregation. Content and taxonomy set to 'groups results together' and count set to aggregation: count.

Relationships:
- taxonomy term [but not required]

Display 2: View block [and is added to a node via the block layout]:
- Format: table
- The fields / filter / sort / relationship settings look like they are the same as the charts content - it looks like this is a table version of the chart data and this does display on the node is it attached to.

I think the view is ok and when I comment out const globalOptions = drupalSettings.charts.google.global_options and so on as outlined in the first post, the chart displays where & how it is expected.

Are the drupalSettings.charts.google.global_options defined in web/modules/contrib/charts/modules/charts_google/src/Plugin/chart/Library/Google.php ?

    // Setting global options.
    $element['#attached']['drupalSettings']['charts']['google']['global_options'] = [
      'useMaterialDesign' => !empty($this->configuration['use_material_design']) ? 'true' : 'false',
      'chartType' => $element['#chart_type'],
    ];

I am sure this attach is working as is should as a few lines above this we have:
$element['#attached']['library'][] = 'charts_google/google';
Which adds web/modules/contrib/charts/modules/charts_google/js/charts_google.js to the page and I can comment out the drupalSettings.charts.google.global_options that prevents the chart from rendering.

Just the drupalSettings option is not getting through? Should there be something 'global_options' in the DB? I did a DB search for "global_options" and the only value returned was in table key-value:
Collection: post_update
Name: existing_updates
Value: string was part of: charts_highcharts_post_update_set_default_global_options
I did not see a google related one, should there be something similar or have is gone off on a random tangent? :)

Would going back to an earlier 5 version be any use? For example, roll back to the DB with v3.5 installed and then install the first v5 release and update through each release to 5.0.8 from there? Or are all the updates from the first 5 release in the 5.0.8 installation?

Thank you for your help

andileco’s picture

I think if you've checked and unchecked the "Use Material Design" box in /admin/config/content/charts, then everything you should need should be passed to the JS. Can you try keeping the default charting library as Google, enabling "Use Material Design", and then try opening your chart either in an incognito tab or in another browser? I can't see any reason why this wouldn't work for you.

ice70’s picture

Hi @andileco

/admin/config/content/charts
- default charting library as Google
- enabling "Use Material Design"
and saved.

Still get the same error [in incognito / hard reload /drupal cache cleared]: Cannot read properties of undefined (reading 'google')

I have a local patch which does the following:

      //const globalOptions = drupalSettings.charts.google.global_options;
      let useMaterialDesign = false;
      let chartType = 'line';

And with this I can get the charts rendering. As the chart settings are defined in the view and they seem to be respected [my testing view has a barm not line chart], I think I should be ok to leave this in place? At least for now?

Thank you for your help :)

flyke’s picture

I am creating a custom dashboard full of Google charts.
After a number of tests and working generated charts, I got the same problem and error as in this issue description.

I notice a few things about this module:
In modules/contrib/charts/modules/charts_google/charts_google.libraries.yml the charts_google library is missing the following dependencies:

    - core/drupal
    - core/drupalSettings

So it should be updated to:

charts_google:
  version: 1.x
  js:
    js/charts_google.js: { weight: -1 }
  dependencies:
    - core/drupal
    - core/drupalSettings
    - charts/global
    - core/once

The file modules/contrib/charts/modules/charts_google/js/charts_google.js should include drupalSettings, change this:

(function (Drupal, once) {
...
}(Drupal, once));

into:

(function (Drupal, drupalSettings, once) {
...
}(Drupal, drupalSettings, once));

For me I noticed that for what I am doing, my code did not need to be dependant on the charts_google/charts_google library, only on the charts_google/google library so in the end I was able to not solve but get around this problem by changing the dependency in my custom module to charts_google/google.

flyke’s picture

I've created a patch for the 5.0.x-dev version.
The patch just provides a fallback value in case drupalSettings.charts is undefined.
It's not a perfect solution I think because it would be more important to figure out why drupalSettings.charts is undefined andfix that.
But if like me you are stuk in your project on semi-unrelated javascript because of this error, then this patch provides a way to move forward.

I've also fixed the missing dependencies in the libraries.yml file and added the drupalSettings to the javascript file.

flyke’s picture

Version: 5.0.8 » 5.0.x-dev

  • andileco committed a7aa06b1 on 5.0.x authored by flyke
    Issue #3391918 by flyke, andileco, ice70: Update from 3.5 to 5.0.8: JS...
andileco’s picture

Status: Active » Fixed

Thank you - nice patch! I am marking fixed, as this makes sense and these are good dependencies. @ice70 if you download the latest dev and still have an issue, please let me know, and I can re-open.

Status: Fixed » Closed (fixed)

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