Skip to content

Instantly share code, notes, and snippets.

@liberatr
Forked from reinis-kinkeris/my_module.module
Last active September 23, 2022 22:24
Show Gist options
  • Save liberatr/856be5c7e37badb366f02ba612ebc1dc to your computer and use it in GitHub Desktop.
Save liberatr/856be5c7e37badb366f02ba612ebc1dc to your computer and use it in GitHub Desktop.
Drupal 8 - Example of adding dynamic js libraries which require locale
/**
* Implements hook_library_info_build().
*
* @return array
*/
function mymodule_library_info_build() {
$libraries = [];
$api_key = \Drupal::config('mymodule.api_key');
$libraries['mymodule.map'] = [
'js' => [
'https://www.googleapis.com/geolocation/v1/geolocate?key=' . $api_key => [],
],
];
return $libraries;
}
/**
* Implements hook_page_attachments().
*/
function mymodule_page_attachments(array &$page) {
$page['#attached']['library'][] = 'mymodule/map';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment