Saturday, January 23, 2016

To include the custom or external js css files

drupal_add_js and drupal_add_css have been deprecated in Drupal 8. The new way to include the Javascript or css files in Drupal 8:

# vim myexample.libraries.yml

this_is_test_lib_name:
  version: 1.x
  css:
    theme:
      css/myexample.css: {}
  js:
    js/myexample.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings

# vim myexample.module

<?php
/**
 * Implements hook_preprocess_page().
 */
function myexample_preprocess_page(&$variables) {
  $variables['#attached']['library'][] =  'myexample/this_is_test_lib_name';
}
?>

# vim myexample/js/myexample.js

(function ($) {
  console.log('ready');
})(jQuery);

or

(function ($, Drupal, window) {
  console.log('ready');
})(jQuery, Drupal, window);

Note: underscore.js and backbone.js are in core in Drupal 8.
Note: One other change you may have noticed: Drupal.settings is now drupalSettings.

Reference:

http://atendesigngroup.com/blog/looking-at-drupal-8-javascript-changes

No comments: