If you are writing your own module in Drupal, you often need to include external or inline CSS / JS through your module code. In Drupal 7 it was typically done using the below approaches:
CSS
1. Using drupal_add_css():
2. Using #attached:
Attaching External Css and Js in Drupal 8
Similar for JS
1. Using drupal_add_js():
2. Using #attached:
For more detail about these you can check the api on:
- http://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#attached
- http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_process_attached/7
The above things would help you to add the required js and css files to your form or page.
But in Drupal 8, these function are deprecated and we cannot attach our css and js files directly using #attached parameter.
If you try to do so in Drupal 8 it would throw a White Screen of Death giving an error in the php log:
So in order to add css and js in Drupal 8 we need to create libraries, so that we can attach the required js and css files
Steps to add libraries:
1. We create a module_name.libraries.yml file having list of all the jss and css that we wish to use.
This would add two libraries in your module.
2. In order to attach the required js and css to your page/form, we simply replace
with key would be the key that you have defined in module_name.libraries.yml file.
So this way you can create multiple entries in your libraries.yml file and attach those libraries at specific position.