修改drupal中的节点在查看时的外观 [英] Modify how a node in drupal will look when viewed

查看:96
本文介绍了修改drupal中的节点在查看时的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文本存储在自定义内容类型的body字段中。身体字段中存储的内容不完全是我在查看节点时要看到的内容。无论是完整的还是作为传情。例如,我可能会存储这样的东西:

I have some text stored in the body field of a custom content type called 'protocol'. What is stored in the body field is not exactly what I want to be seen when you view the node. Either as full or as a teaser. Forexample i might store something like this:

[用法]可重复使用DNA模板微阵列进行无细胞合成的蛋白质排列[/ usage]
[安全考虑]测试安全考虑[/安全考虑]

"[usage]Repeatable use of DNA template microarry for protein arraying by cell-free synthesis.[/usage] [security considerations]Test security considerations[/security considerations]"

但是,当您查看时,我想要显示的是:

But what I want displayed when you view it would be this:

可重复使用DNA模板微阵列通过无细胞合成进行蛋白质排列
测试安全性考虑

"Repeatable use of DNA template microarry for protein arraying by cell-free synthesis. Test security considerations"

我教过这是什么你可以使用hook_view,所以我在我的模块中尝试了这个功能:

I taught this was what you could use hook_view for, so I tried this function in my module:

function molmethtag_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'protocol2' && $view_mode=='full') {

    $body = $node->body;
    $prot_array = _molmethtag_extract_data($body['und'][0]['value']);
    $protocol = '';
    foreach($prot_array as $tag => $prot_row) {
      $protocol .= "<div id='molmethtag_".$tag."'>".$prot_row."</div>";
    }                                                                                                                  
    $body['und'][0]['value'] = $protocol;

  }
}

但这没有办法像我想要的我希望保存在协议中的文本将在查看时显示为正文,但显示原始文本。

But this didn't work out like I wanted. I hoped that the text saved in protocol would be displayed as the body when viewing but it showed the original text.

有没有人知道我做错了什么?这个hook_view是不是用的?我应该使用/做什么?

Does anybody know what I've done wrong? Is hook_view not used for this? What should I use/do instead?

推荐答案

起初有点混乱,但您需要更改显示的值在 $ node-> content array:

It's a bit confusing at first but the values you need to change for display are on the $node->content array:

$node->content['body'] = array(
  '#markup' => $protocol
);

hook_node_view()这只是为了完整起见,你也可以在 hook_preprocess_node()中做类似的事情,如果你想。

hook_node_view() is a great place for this, but just for the sake of completeness you could also do something similar in hook_preprocess_node() if you wanted to.

这篇关于修改drupal中的节点在查看时的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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