Using external JS libraries in our Drupal 8 module

And doing it right

Kevin Porras
MANATI | Agencia Web
4 min readMay 26, 2017

--

Resulting chart for our Chart.js example

También disponible en español aquí.

In order to do this in Drupal 7 we use the libraries module. For Drupal 8, this keeps being the right way to do it; however, this module is in a huge change process and it's not totally well documented, that's why making this the first time is a real challenge.

For this post, we're going to use the Chart.js library in a custom block. First thing we need to do is downloading and installing libraries module. Once done that, according to the documentation we need to check if there's a definition for the library we want to use in the libraries registry; if it exists and works for us, we save one step. If it doesn't exist (that was my situation), we need to create it (and contribute it back to the community of course). To do that, we have to create a json file with our library definition in the folder public://library-definitions. For our case this file will be named chart.js and will have this content:

In this file, we write the library human name, library type (asset), and some other library information.

Please remember to contribute the file through the issue queue once you have tested it so that other people can benefit from it.

The next step is to download the library and place it in the right folder; it's in sites/all/assets/vendor/chart_js for our case. There, we should place Chart.js and Chart.min.js

Of course, in order to use that library, we need a custom js file; so, we create one inside a js folder (only for order) with the name we want. For this post, we're following Chart.js basic example; so, our file will be named demo_chartjs.js and will have this:

There are no differences in the way javascript works in Drupal 8 in comparison with Drupal 7. If you want, you can check out the documentation.

For Drupal 8, the only stuff you can attach to any render array are libraries, so we need to define a library. To do that, according to the official documentation we need to create a .libraries.yml file. Since our module is named demo_chartjs, this file will be named demo_chartjs.libraries.yml and have this content:

Next, we need to create a template to render the necessary markup for our chart. So, we're going to implement hook_theme in the .module file:

And add our template file in a templates folder inside our module. That file will be named demo-chartjs-demo-chart.html.twig and have this content:

Now, the only pending thing is to create our block; so, let's create a file DemoChartJs.php in src/Plugin/Block and place this content there:

The most important part of that file is the build function; there, we're instructing Drupal to use the theme demo_chartjs_demo_chart previously defined as the rendering of our template. We're also attaching two libraries:

  • libraries/chart_js: Chart.ks library. Since this is an external library; they are always registered as part of the libraries module.
  • demo_chartjs/demo_chartjs: Our libary as defined in .libraries.yml file. This one includes only our js file.

With all the previous stuff done; it's time to test our block. Go ahead and place it in the site and you should have a pretty chart as shown in the header image of this post.

Disclaimer: libraries module is in continous development so some things could change; however, the general concepts shown here I think will remain the same.

I hope this be useful for you.

Manatí is a web consultancy firm from Costa Rica, where we make websites strategically designed to help organization achieve goals.

Don’t miss out on projects, news and ideas from Manatí. Learn about what we do, follow us on medium, twitter and facebook.

--

--

MANATI | Agencia Web
MANATI | Agencia Web

Published in MANATI | Agencia Web

We’re solution makers. We make websites strategically designed to help your organization achieve goals.

Kevin Porras
Kevin Porras

Written by Kevin Porras

Drupal, DevOps, Backend, PHP...

No responses yet