Skip to content

Instantly share code, notes, and snippets.

@btmash
Created February 13, 2018 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btmash/96a241ffdd5cb9ebbaa7c9236e7771e7 to your computer and use it in GitHub Desktop.
Save btmash/96a241ffdd5cb9ebbaa7c9236e7771e7 to your computer and use it in GitHub Desktop.
This adds libraries conditionally in d8
name: 'Ashok Test'
type: module
description: Test loading libraries conditionally
core: 8.x
ashok_library_one:
version: 1.0.1
js:
ashok_js_one.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
ashok_library_two:
version: 1.33.7
js:
ashok_js_two.js: {}
dependencies:
- core/jquery
- core/drupal
- core/drupalSettings
<?php
/**
* Implements hook_page_attachments().
*/
function ashok_page_attachments(array &$page) {
// Load the library on every page.
$page['#attached']['library'][] = 'ashok/ashok_library_one';
$current_path = \Drupal::service('path.current')->getPath();
$path = '/node/1';
// Load the library on every page BUT '/node/1'. This is an explicit path.
if (!\Drupal::service('path.matcher')->matchPath($current_path, $path)) {
$page['#attached']['library'][] = 'ashok/ashok_library_two';
}
}
console.log('This is js 1');
console.log('This is js 2');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment