0

我已经设置了一个具有自定义节点类型的模块(我在我真正喜欢使用的 javascript jQuizMe 之后调用了 jquizme)。我为我需要提供的 javascript 文件设置了两个字段以使其工作(在一般 jQuizMe-2.2.js 文件之后,您需要添加另外两个 javascript 文件 - 一个用于设置,一个用于测验内容)。

Drupal 将文件保存为 myjavascriptfile.js.txt - 我对它们进行了测试,它们仍然可以制作 jQuizMe 界面 - 好的。问题是,我想在节点页面上添加这些文件......每个节点的文件都不同。如何访问 drupal_add_js() 函数的文件,以便他们为相关节点加载文件?

我尝试设置自定义字段格式化程序,但我不知道如何自动访问给定节点的文件的 uri 以放入 drupal_add_js() 函数(我可以添加一个静态文件,它加载得很好......我使用 hook_node_view ( jquizme_node_view ) 做到了这一点。

所以我只需要一种访问文件信息的方法......它们如何链接到每个节点?我找不到连接。正如您可能注意到的那样,我是一个编写模块的新手,我可能不太了解与面向对象编程相关的内容,抱歉,还没有进步到那个水平),但我愿意接受任何答案。我确定我遗漏了重要信息,但这已经太长了。

我之前还设置了一个特殊页面,看看我是否可以让 jQuizMe 在 Drupal 中工作,这样它仍然在代码中。

我已经尝试了很多答案(过去六个小时左右......在这里说太多了),其中最新的是使用令牌,但这不起作用。这是我到目前为止所拥有的:

function jquizme_node_view($node, $view_mode, $langcode) { switch ($node->type) { case 'jquizme': $items = field_get_items('node', $node, 'field_myfield', $node->language); drupal_add_css(drupal_get_path('module', 'jquizme') . '/jQuizMe.css', >array('scope' => 'header')); drupal_add_js(drupal_get_path('module', 'jquizme') . '/alert.js'); drupal_add_js(drupal_get_path('module', 'jquizme') . '/jQuizMe-2.2.js', >array('scope' => 'header')); //drupal_add_js($tokens['node']['jquizme_js1_field'], array('scope' => >'header')); //drupal_add_js($tokens['node']['jquizme_js2'], array(' 范围' => '标题')); 休息; } }

提前致谢!

4

1 回答 1

0

你能试试这个吗?

// Let me assume that the field names of your two file fields
// are jquizme_js1_field and jquizme_js2_field for convenience..
function jquizme_node_view($node, $view_mode, $language) {
  $items = field_get_items('node', $node, 'jquizme_js1_field');
  _jquizme_add_js_from_filefield($items);

  $items = field_get_items('node', $node, 'jquizme_js2_field');
  _jquizme_add_js_from_filefield($items);

}

// Given the values of a filefield attached to some entity,
// adds them as JS files to the page.
function _jquizme_add_js_from_filefield($items = array()) {
  foreach ($items as $item) {
    $fid = &$item['fid'];
    $file = file_load($fid);
    if (!$file) {
      continue; // Maybe the file got deleted..
    }
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
    $path = $wrapper->realpath();
    // Ensure that the path exists and that it is a Javascript file..
    if (file_exists($path) && preg_match('\.js$', $path)) {
      drupal_add_js($path, array('type' => 'file'));
    }
  }
}
于 2012-06-18T14:18:58.703 回答