drupal - 如何在 Drupal 8 中的外部 js url 中传递动态参数

标签 drupal

我们需要在 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参数。

提前致谢。

最佳答案

用于添加动态库定义。

使用hook_library_info_build()功能

/**
 * 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/

相关文章:

forms - 在 Drupal 6 中向管理员添加系统变量的编辑功能

php - 如何将整个站点从 HTTP 重定向到 HTTPS(Drupal 站点中除了 2 个页面之外)?

php - 我什么时候应该使用 CMS 而不是从头开始创建网站?

drupal - 创建自定义搜索表单 drupal

php - 如何限制 drupal 搜索索引所有内容类型?

drupal - 如何以编程方式在 Drupal 7 中注销用户?

php - Drupal 7 EntityFieldQuery 分类字段的字段条件(多个值)不在我的排除数组中

Drupal Rules 一次性登录地址

drupal - 如何在不使用 API 的情况下向 Drupal 6.x 注册用户?

javascript - Grid Stretch 横跨整个浏览器。