将 HTML 添加到 Drupal 模块中的节点标题,而不是主题层中
我想向我的 Drupal 6 模块添加一些功能:我想在某些节点标题旁边插入图像(它涉及 span 和 img 标签)。我想我可以通过实现 mymodule_preprocess_node() 并修改标题来做到这一点。然而,Drupal 会删除所有标签以避免 XSS 攻击。
我见过许多涉及主题层的解决方案(最常见的是 http://drupal.org/node/28537),但我想在我的模块中而不是在主题中执行此操作。我不想修改任何 .tpl.php 文件或 template.php。谁能给我一些如何做到这一点的提示?
I want to add some functionality to my Drupal 6 module: I want to insert an image next to certain node titles (it involves a span and an img tag). I thought I could do this by just implementing mymodule_preprocess_node() and modifying the title. However, Drupal strips out all tags to avoid XSS attacks.
I've seen many solutions that involve the theme layer (most commonly http://drupal.org/node/28537), but I want to do this in my module, not in the theme. I don't want to modify any .tpl.php files or template.php. Can anyone give me tips on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您提到您已经尝试过 preprocess_node(),并且正确的是,如果您将 img 标签存储为节点标题的一部分,Drupal 确实会将其删除,因为它通过 < 中的 check_plain 运行 $node->title a href="http://api.drupal.org/api/drupal/includes--theme.inc/function/template_preprocess_node/6" rel="nofollow">template_preprocess_node()。
我的建议是将实际图像存储为图像字段(使用 imagefield 模块和 < a href="http://drupal.org/project/imagecache" rel="nofollow">imagecache 用于调整大小),将该字段的显示设置为在给定内容类型的 CCK 显示选项卡上隐藏,然后将图像附加到模块预处理函数中的 $title 变量中。此解决方案还允许您在您可能需要创建的任何视图中的标题旁边显示该图像。
You mention that you've tried preprocess_node(), and are correct that, if you are storing the img tag as part of the node title, Drupal will indeed strip that out, as it runs $node->title through check_plain in template_preprocess_node().
My suggestion would be to store the actual image as an image field (using some combination of the imagefield module and imagecache for sizing), set the display of that field to be hidden on the CCK display tab for the given content type, and then attach the image to be part of the $title variable in your module's preprocess function. This solution would also allow you to display that image next to the title in any views you may need to create.
“某些节点标题”-您的意思是来自某些节点类型的所有节点标题吗?
如果是这样,您可以仅使用 CSS 来设置节点的样式。默认情况下,所有节点都有一个与节点类型相对应的类。使用 CSS 背景图像,您可以将图像添加到节点标题。
您的模块可以调用 drupal_add_css 并添加将任何必需的 CSS 添加到页面中。这样,它就与主题无关了。
By 'certain node titles' - do you mean all nodes titles from certain node types?
If so, you can likely style the node using only CSS. By default all nodes will have a class that corresponds to the node type. Using CSS background images, you can add an image to the node title.
Your module can call drupal_add_css and add in any required CSS into the page. This way, it is theme independent.
我认为更简单的方法是使用 javascript / Jquery。
您创建一个仅在某些类型的节点中调用的 Jquery 脚本,并将视图数量从 drupal 传递到 jscript。
您可以在 module_preprocess_node 中调用 drupal_add_js() 并将包含视图数量或图像链接的变量传递给脚本。像这样:
然后在 my_js_file.js 中更改文本。有几种方法可以实现这一点。例如,您可以在文档中进行搜索并将标题更改为其他内容,或者您可以使用确定的 ID 等...
使用 jQuery 查找文本字符串?
http://api.jquery .com/replaceWith/
I think the easier way is with javascript / Jquery.
You create a Jquery script which is called only in certain types of nodes and pass the number of views from drupal to the jscript.
You can call drupal_add_js() inside your module_preprocess_node and pass a variable which contains the number of views or the image link to the script. Something like this:
then in my_js_file.js just change the text. There are several ways to acomplish this. For instance, you can do a search in the document and change the title to something else or you can use a determined ID, etc...
Find text string using jQuery?
http://api.jquery.com/replaceWith/