Score:1

Why isn't the Highcharts library, defined from a custom module, loaded in every page?

ae flag

I am running Drupal 9.1.7 on lando on a Mac. I am writing a custom module and I want to include the Highcharts library. The .libraries.yml file looks like this.

stats_degrees:
  js:
    https://code.highcharts.com/highcharts.js: { type: external, minified: false }
  dependencies:
    - core/jquery

From reading the documentation, I believe I should see the Highcharts library at the bottom of every page when I view the source, but I don't see it anywhere.

Am I missing something?

I have already validated the YAML content. I also installed the Charts module, if that makes any difference.

Score:2
in flag

Defining the library is only half of the process. Libraries are not loaded on every page by default - you need to tell Drupal when it should include your library.

For full details, see the Attaching a library to page(s) section of the Adding stylesheets (CSS) and JavaScript (JS) to a Drupal module documentation on drupal.org.

I bet you want to attach the library to a render array:

$build['the_element_that_needs_asset_library']['#attached']['library'][] = 'your_module/library_name';

Or attach the library in a twig template (the template that produces your chart markup):

{{ attach_library('your_module/library_name') }}
Dorian Winterfeld avatar
ae flag
Yes! I am trying to attach it to a render array like so: `$chart['#attached']['library'][] = 'stats_degrees/highcharts';` `$chart['#type'] = 'chart';` `$chart['#chart_type'] = 'line';` `$chart['#chart_library'] = 'highcharts';` etc. But when I return the array: `return render($chart);` it is always null and there is no Highcharts JS when I view source. This approach worked in D7 but D7 handled JS libraries differently and drupal_render() has been replaced by render().
sonfd avatar
in flag
You usually shouldn't render a render array by calling render, you should just return it. That's the first thing that I would try. See https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-best-practices-preprocess-functions-and-templates
Dorian Winterfeld avatar
ae flag
I am doing it for debugging in PHP Storm, `$rendered = \Drupal::service('renderer')->renderPlain($chart);`. When I drill into the debugger I see that Renderer::executeInRenderContext() is returning null. There must be something amiss with my render array... maybe we should close this thread and I will open a new one after I do more debugging.
sonfd avatar
in flag
Yeah, that seems like a good idea. It sounds like you know how to attach the library properly, but there's a problem with your render array.
Dorian Winterfeld avatar
ae flag
I installed the highcharts library using composer. It installed it under /libraries. Makes sense. How do I reference this in my .libraries.yml file? I tried making it relative to my module `../../../libraries/highcharts/highcharts.js: { }` but I still don't see it in the page source. I have tried attaching it to my render array and in a twig template like this: `{{ attach_library('stats_degrees/highcharts') }}` but I still don't see it.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.