如何在 Drupal 8 主题中添加 javascript 库? [英] How to add javascript library in Drupal 8 theme?

查看:24
本文介绍了如何在 Drupal 8 主题中添加 javascript 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我正在构建的 Drupal 8 主题中包含一个自定义 javascript 文件来操作我的菜单.我按照 Drupal 8 主题指南中的说明进行操作,将其包含在我的 .info.yml 文件中:

I'm attempting to include a custom javascript file to manipulate my menu's in the Drupal 8 theme I am building. I followed the instructions in the Drupal 8 themeing guide, including it in my .info.yml file:

#js libraries
libraries:
- dcf/base

并在我的 .libraries.yml 文件中定义它:

and defining it in my .libraries.yml file:

base:
version: 1.x
js:
    js/endscripts.js
dependencies:
    - core/drupal
    - core/underscore
    - core/backbone
    - core/jquery

最后创建一个带有钩子实现的 .theme 文件(我不是真正的 PHP 开发人员,所以我主要是从指南中的示例中进行复制/粘贴工作)

and finally creating a .theme file with a hook implementation (I'm not really a PHP developer, so I mostly did a copy/paste job from the example in the guide)

<?php

function dcf_page_alter(&$page) { 
  $page['#attached']['library'][] = 'dcf/base';
}

?>

我清除缓存,注销以查看未登录的页面(管理视图有很多额外的脚本和它调用的 css 文件)并查看源代码以查看我的脚本是否正在加载.我在库中列出的所有依赖项都被加载,但不是我的脚本本身.

I clear the cache, log off to see the non-logged-in page (the admin view has a LOT of extra scripts and css files that it calls) and look at the source to see if my script is being loaded. All of the dependencies I listed in my library are being loaded, but not my script itself.

脚本本身只是一个基本测试,它应该使用 JQuery 隐藏我的主菜单,将主题设置为主题指南提到的严格"格式是必需的.

The script itself is just a basic test that should hide my main menu using JQuery, put into the 'strict' format the themeing guide mentions is required.

(function () {
  "use strict";
  // Custom javascript
  $(document).ready(function() {
    $("#block-dcf-main-menu").hide();
  });
})();

此时我不知所措.我的直觉是错误在于我的钩子实现,因为我真的不了解 Drupal 的钩子系统,但据我所知,它仍然可能只是他们还没有为 Drupal 8 实现这个(我这样做是为了为我的组织即将进行的网站重建测试 Drupal 8,并且我目前正在运行 Drupal 8.0.x-beta2,但未安装其他模块)

I'm at a loss at this point. My instinct is that the mistake is in my hook implementation, because I really don't understand Drupal's hook system, but as far as I can tell, it could still just be that they haven't finished implementing this yet for Drupal 8(I'm doing this to test Drupal 8 for my organizations upcoming website rebuild, and am currently running Drupal 8.0.x-beta2 with no additional modules installed)

推荐答案

答案,如来自 clive 的评论一样,是脚本末尾缺少的 {}.所以 mytheme.libraries.yml 中的脚本声明应该是
js:js/endscripts:{}

The answer, as in the comment from clive was the missing {} at the end of the script. So the script declaration in mytheme.libraries.yml should be
js: js/endscripts: {}

这篇关于如何在 Drupal 8 主题中添加 javascript 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆