Created
February 13, 2018 21:36
-
-
Save btmash/96a241ffdd5cb9ebbaa7c9236e7771e7 to your computer and use it in GitHub Desktop.
This adds libraries conditionally in d8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'Ashok Test' | |
type: module | |
description: Test loading libraries conditionally | |
core: 8.x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('This is js 1'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('This is js 2'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment