我们需要在 modulename.libraries.yml 文件中的外部 JS(Google MAP API URL)中传递动态参数(API Key)。因为,API Key 由管理员存储在 Drupal Config 表单中。
模块名称.libraries.yml:
google.mapaddr:
js:
<a href="https://maps.googleapis.com/maps/api/js?key=" rel="noreferrer noopener nofollow">https://maps.googleapis.com/maps/api/js?key=</a><strong>[API-KEY]</strong>&libraries=places: { type: external }
请指导我们在Drupal 8标准中将Config变量传递给External JS参数。
提前致谢。
最佳答案
用于添加动态库定义。
/**
* Implements hook_library_info_build().
*/
function my_module_library_info_build() {
$config = \Drupal::config('my_module.settings');
$api_key = $config->get('my_module.api_key');
$libraries = [];
$libraries['my_module_js'] = [
'version' => '1.x',
'header' => true,
'js' => [
'https://my.external.com/' => [
'type' => 'external',
'minified' => true,
'attributes' => [
'key' => "$api_key",
'async' => "async",
],
],
],
];
return $libraries;
}
希望对您有用。
关于drupal - 如何在 Drupal 8 中的外部 js url 中传递动态参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43590060/