有没有一种简单的方法来实现 jQuery 插件作为 Drupal 内容节点的一部分?
我有一个自己编写的 jQuery 插件,我希望它只出现在我的 Drupal 6 网站的一个节点上。我有 FTP 和 Linux shell,可以用来将文件上传到站点,但问题实际上是将它们附加到节点。将 JavaScript 放入节点的 Body 中可能会变得非常难看,尤其是当 Body 以 WYSIWYG 编辑器呈现时(乱七八糟的缩进、WYSIWYG 试图用
标签包装我的所有
是否有任何类型的Drupal 插件或任何类型的解决方法可以使这种集成更容易?
编辑:
我尝试过 Code Per Node,这对于将 JavaScript 与正文分离非常有用,但我真的需要链接到单独的 JavaScript 文件的选项(此插件需要多个支持文件)。
I have a jQuery plugin that I authored and I want it on only one node of my Drupal 6 web site. I have FTP and a Linux shell I can use for uploading files to the site, but the issue is actually attaching them to a node. Putting JavaScript in a node's Body can get pretty ugly, especially when the Body is presented in a WYSIWYG editor (haywire indentation, WYSIWYG attempting to wrap all my <script> tags with <p> tags, etc)
Is there any sort of Drupal plugin or any kind of workaround to make this kind of integration easier?
Edit:
I've tried Code Per Node, which is great for separating JavaScript from the Body, but I really need the option to link to separate JavaScript files (this plugin requires several support files).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在项目中执行此操作的方法是将 CCK 文本字段添加到名为“其他资源”的内容类型,该内容类型接受多个值。然后,在节点中,我在此字段中添加了多个值——我想要在该节点上加载的 CSS 和 JS 文件的路径。然后,在我的主题中,我在
template.php
中添加了自己的函数,名为themename_node_process_fields
。该函数是在node-content_type_name.tpl.php
中执行的第一件事。除此之外,它还做了以下事情:请注意,我还没有考虑过是否有任何安全考虑。我是唯一有权访问该字段的人,但如果恶意用户能够输入任意文本,我不确定可以做什么。
The way I did this in a project was to add a CCK text field to the content type called "Additional Resources" that accepts multiple values. Then in the node I added multiple values in this field -- the paths to CSS and JS files I wanted to load on that node. Then, in my theme I added my own function called
themename_node_process_fields
intemplate.php
. That function was the first thing executed innode-content_type_name.tpl.php
. Among other things, it did this:Note that I have not thought through if there are any security considerations for this. I'm the only one that has access to that field, but if a malicious user were able to enter arbitrary text I'm not sure what could be done.
如果您不寻找管理工具,可以使用
drupal_add_js
函数。它将在渲染时将 JavaScript 文件插入到页面中。但是,这需要激活 PHP 输入过滤器。从链接:
If you aren't looking for an administrative tool, you can use
drupal_add_js
function. It will insert the JavaScript file into the page at render time. However, this requires having the PHP input filter activated.From the link:
最好的方法是制作自己的使用该插件的模块。这就是“Drupal”的方式。您还可以使用 drupal_add_js 将脚本附加到模块。如果它只是一个特定的节点,您需要附加这个插件,我要么在模板中创建一个 if 语句来查找该节点,但这样做的方式有点丑陋。
The best way would be to make your own module that uses the plugin. That is the "Drupal" way to do it. You can also you drupal_add_js to attach scripts to the module. If it is only a specific node you need to attach this plugin to I would either make a if statement in the template looking for that node, kinda an ugly way to do it though.