【问题标题】:Drupal 8 - Loading a library globally from a custom moduleDrupal 8 - 从自定义模块全局加载库
【发布时间】:2016-10-03 21:13:19
【问题描述】:

我已经创建了库(example-libraries.yml):

example:
  version: VERSION
  js:
    js/example.js: {}
  css:
    theme:
      css/example.css: {}

我尝试通过这样做(example.info.yml)全局加载它:

name: Example Module
type: module
description: "A module that is responsible for ..."
package: Custom

core: 8.x
version: 1.0-SNAPSHOT

libraries:
  - example/example

我可以通过执行以下操作为特定表单成功加载它:

$form['#attached']['library'][] = 'example/example';

知道如何让它在全球范围内工作吗?

【问题讨论】:

    标签: drupal drupal-modules drupal-8


    【解决方案1】:

    我终于明白了。我的自定义模块将放置一个额外的选项卡作为 system.admin 菜单的子项。每个其他选项卡都有一个图标,我也想将我的自定义模块图标放在那里。

    如果其他人遇到同样的问题,我必须在文件 example.module 中创建一个条目:

    /**
     * Implements hook_toolbar().
     */
    function example_toolbar() {
        $items['example'] = array(
            '#type' => 'toolbar_item',
            '#attached' => array(
                'library' => array(
                    'example/example',
                ),
            ),
        );
    
        return $items;
    }
    

    【讨论】:

      【解决方案2】:

      在您添加的 my_theme.info.yml 中非常简单:

      libraries:
      

      - 示例/示例

      https://www.drupal.org/docs/8/theming-drupal-8/defining-a-theme-with-an-infoyml-file

      【讨论】:

      • 我需要为此创建自定义主题吗?我的意图是只有一个模块。
      • 是的,您需要自定义主题。创建它既简单又快速。 drupal.org/docs/8/theming
      【解决方案3】:

      您不需要为此自定义主题。使用hook_page_attachments()

      /**
       * Implements hook_page_attachments().
       */
      function example_page_attachments(array &$attachments) {
        $attachments['#attached']['library'][] = 'example/example';
      }
      

      【讨论】:

      • 我是 Drupal8 的新手,就目前而言,我的全局设置都在 yml 文件中。我应该在哪里添加那段代码,以便我的库可以在全球范围内使用?
      • 把它放到你的example.module文件中
      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 2020-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2018-06-21
      • 2015-08-03
      • 2020-05-06
      相关资源
      最近更新 更多