如何在独立于主题的节点页面上添加css和js文件? [英] How to add css and js files on node pages independent of the theme?

查看:168
本文介绍了如何在独立于主题的节点页面上添加css和js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用显示套件显示类型为文章的完整节点。我想在包含文章节点的页面上添加一些CSS和JS,虽然添加这些不能取决于主题。所以我不能使用template.php文件

I am using display suite to display a full node of type 'Article'. I want to add some css and js on the page containing the article node, though the adding of these can't be dependent on the themeing. So I cannot use the template.php file

我如何能做到这一点?

推荐答案

创建一个新模块并放在:/ sites / all / modules / custom

Create a new module and put it in: /sites/all/modules/custom

模块结构和文件如下所示:

The module structure and files would look like this:

ahelper/
ahelper/css/article_node.css
ahelper/js/article_node.js
ahelper/ahelper.info
ahelper/ahelper.module

ahelper / ahelper.info

ahelper/ahelper.info

core = "7.x"
name = "Article Helper"
project = "ahelper"
version = "7.x-1.0"

ahelper / ahelper.module

ahelper/ahelper.module

<?php
/**
 * Implements hook_node_view()
 */
function ahelper_node_view($node, $view_mode, $langcode) {
  // if node is an article, and we're looking at a full page view
  if ($node->type == 'article' && $view_mode == 'full') {
    // then add this javascript file
    drupal_add_js(drupal_get_path('module', 'ahelper') .'/js/article_node.js');
    // and add this css file
    drupal_add_css(drupal_get_path('module', 'ahelper') .'/css/article_node.css');
  }
}

然后只需启用模块。你可以使用node_view钩子。

Then just enable the module. You can play around with the node_view hook.

这篇关于如何在独立于主题的节点页面上添加css和js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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