How do we use drupal_add_library(), drupal_add_css() and drupal_add_js()? How do I use drupal_add_library(), drupal_add_css() and drupal_add_js() in Drupal 8?

I am porting a module that uses those functions. I am following what reported in this post, which says these functions have been removed in favor of #attached, but I am not able to attach these files to my configuration page.

The error I am getting is the following:


  Uncaught PHP Exception LogicException: "You are not allowed to use js in #attached"


How do I attach CSS and JavaScript to a page?
Drupal_add_js() and Drupal_add_css() are quite simple to write in Drupal 8.

We basically create a module_name.libraries.ymlfile having list of all the jss and css that we wish to use.

An example of which is 

name_of_the_key: // This would basically work as a key for you.
  version: 1.x
  js:
    js/name_of_your_js_file.js: {}  // Name of your js file
  dependencies:  // Adding dependencies if you have any.
    - core/jquery
    - core/drupalSettings
  css:
    theme:
    css/name_of_the_css_file.css: {} // Name of your css file.


This would basically add all the files but would not attach these files to your page/form.

So in order to attach the js and css to your page/form, you simply replace 

drupal_add_js()
drupal_add_css()


with

$form['#attached']['library'][] = 'name_of your_module/name_of_the_key'; // Name of the key would be the key that you have defined in module_name.libraries.yml file.


So if your module uses different js and css file at different points in a forms/pages you could define n number of keys in your libraries.yml file and use it accordingly.

Source: https://www.drupal.org/node/2274843 and guidance form AjitS.