【问题标题】:Drupal 8 Creating a moduleDrupal 8 创建模块
【发布时间】:2016-08-30 12:17:10
【问题描述】:

如何在 drupal8 中创建一个简单的模块,该模块在特定页面中附加一个小的 JS 代码(如谷歌分析)。

最有可能使用管理员设置来选择特定页面选项。

【问题讨论】:

    标签: drupal-modules drupal-8


    【解决方案1】:

    在你的模块中创建一个mymodule.libraries.yml

    mymodule.base:
      version: 1.0.0
      js:
        js/your-javascript.js: {}
    

    那么我们hook_page_attachments

    function photography_page_attachments(array &$page) {
      // Check the request URI or Path to decide if you want to show the library
      //
      // You may use:
      // 1. \Drupal::service('path.current')->getPath()
      // 2. \Drupal::request()->getRequestUri();
      //
      $page['#attached']['library'][] = 'mymodule/mymodule.base';
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 2016-10-05
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多