有没有一种简单的方法来实现 jQuery 插件作为 Drupal 内容节点的一部分?

发布于 2024-10-10 18:30:32 字数 442 浏览 3 评论 0原文

我有一个自己编写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠佳期 2024-10-17 18:30:32

我在项目中执行此操作的方法是将 CCK 文本字段添加到名为“其他资源”的内容类型,该内容类型接受多个值。然后,在节点中,我在此字段中添加了多个值——我想要在该节点上加载的 CSS 和 JS 文件的路径。然后,在我的主题中,我在 template.php 中添加了自己的函数,名为 themename_node_process_fields。该函数是在 node-content_type_name.tpl.php 中执行的第一件事。除此之外,它还做了以下事情:

  // Loop through the additional resources and add them to the <head>.
  if (isset($node->field_additional_resources) && count($node->field_additional_resources) > 0) {
    foreach ($node->field_additional_resources as $resource) {
      if (strpos($resource['safe'], '.css') !== FALSE)
        drupal_add_css($resource['safe'], 'theme', 'all', FALSE);
      else if (strpos($resource['safe'], '.js') !== FALSE)
        drupal_add_js($resource['safe'], 'theme', 'header', FALSE, TRUE, FALSE);
    }
  }

请注意,我还没有考虑过是否有任何安全考虑。我是唯一有权访问该字段的人,但如果恶意用户能够输入任意文本,我不确定可以做什么。

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 in template.php. That function was the first thing executed in node-content_type_name.tpl.php. Among other things, it did this:

  // Loop through the additional resources and add them to the <head>.
  if (isset($node->field_additional_resources) && count($node->field_additional_resources) > 0) {
    foreach ($node->field_additional_resources as $resource) {
      if (strpos($resource['safe'], '.css') !== FALSE)
        drupal_add_css($resource['safe'], 'theme', 'all', FALSE);
      else if (strpos($resource['safe'], '.js') !== FALSE)
        drupal_add_js($resource['safe'], 'theme', 'header', FALSE, TRUE, FALSE);
    }
  }

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.

并安 2024-10-17 18:30:32

如果您不寻找管理工具,可以使用 drupal_add_js 函数。它将在渲染时将 JavaScript 文件插入到页面中。但是,这需要激活 PHP 输入过滤器

...content...
<?php
  drupal_add_js('external_file.js', 'external');
?>
...more content...

从链接:

外部:
添加外部 JavaScript(“外部”):允许包含未托管在本地服务器上的外部 JavaScript 文件。请注意,当预处理开启时,这些外部 JavaScript 引用不会被聚合。

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.

...content...
<?php
  drupal_add_js('external_file.js', 'external');
?>
...more content...

From the link:

External:
Add external JavaScript ('external'): Allows the inclusion of external JavaScript files that are not hosted on the local server. Note that these external JavaScript references do not get aggregated when preprocessing is on.

此岸叶落 2024-10-17 18:30:32

最好的方法是制作自己的使用该插件的模块。这就是“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文