如何将模块附加到 drupal 文章?

How to attach a module to a drupal article?

我在 drupal 模块中有一个 php 函数。此函数输出一些随机文本。我想将这个模块附加到一篇 Drupal 文章中,这样每次有人创建一篇文章时,随机文本就会出现在其中。我怎样才能做到这一点?

这里的解决方案是使用一个Drupal钩子函数来修改节点的内容。

假设您的模块名为 "my_module":,您将向 my_module.module 文件添加另一个函数,如下所示:

function my_module_node_view(&$node, $view_mode, $langcode) {
    // We want to make sure this only applies to nodes of the content type "article"
    if ($node->type == "article") { 
        // Append the output of your function to the body; this could easily be added to any other field as well
        $node->body['und'][0]['value'] = $node->body['und'][0]['value'] . my_module_random_text_function();
    }
}

注意:$node 对象通过引用自动传递给这个钩子函数,所以你不需要担心从函数返回任何东西。

如果您想在主题层应用它,您可以在主题的 template.php 文件中使用 theme_preprocess_node 挂钩,但您最初的问题表明您已经关闭了插件路线.