How to attach a library to a theme?

How to attach a library to a theme?

Attaching a library can be done in several ways depending on your needs. Remember that the library is always registered by its module or theme name followed by the library name. In this case, it would be ‘example/example’. hook_page_attachments() and template_preprocess_HOOK() operate exactly the same way as seen in this example:

How to add JavaScript to a theme or module?

A working theme or module. If you don’t have a theme or module, you should start with Creating custom modules or Theming. There are two basic steps to including JavaScript in your theme or module: Attach the library to an entity. Note: A module/theme name of ‘ example ’ will be used in all examples.

How to attach library to custom form in Drupal?

Then in the src/Form/MyCalcForm.php (where is set the MyCalcForm class), i edited the buildForm method adding this just before the return (): But there’s no js nor css loading.

How to add stylesheets to a Drupal theme?

For themes, see Adding stylesheets (CSS) and JavaScript (JS) to a Drupal theme. In Drupal 8 and later versions, stylesheets (CSS) and JavaScript (JS) are loaded through the same system for modules (code) and themes, for everything: asset libraries.

How to attach a library to a JavaScript file?

The {} after the JavaScript file lets you declare properties for the asset being loaded. Some examples include: Attaching a library can be done in several ways depending on your needs. Remember that the library is always registered by its module or theme name followed by the library name. In this case, it would be ‘example/example’.

How to attach a library to a render array?

To give another example of attaching a library to a render array, If you are building a block plugin in your module, you can attach the libraries to the render array in the build () function of your class extending the BlockBase class (as of Drupal 8 beta 6). As forms are just render arrays, attaching a library works just the same:

How to attach a library to a render array in Drupal?

Attaching to a render array of a Block Plugin To give another example of attaching a library to a render array, If you are building a block plugin in your module, you can attach the libraries to the render array in the build () function of your class extending the BlockBase class (as of Drupal 8 beta 6).