如何在Drupal 8主题中添加javascript库? [英] How to add javascript library in Drupal 8 theme?
问题描述
我正在尝试添加一个自定义的javascript文件来操纵我正在构建的Drupal 8主题中的菜单。我遵循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(I '做这个测试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屋!