Drupal 6 - 为每个动态信息块创建一个模块?

发布于 2024-10-22 06:18:57 字数 317 浏览 1 评论 0原文

我有一个大约 20 页的 Drupal 6 网站。在每个页面中,我需要创建许多小部件,其中的信息要么存储在数据库内,要么来自外部 Web 服务。大多数时候,“视图”(来自视图模块)不足以解决需求。

到目前为止,每当我需要这样的小部件时,我都会创建一个实现 hook_block 的新模块。然后,我将这个新模块拖放到我想要的面板中。我需要创建大约 20 个模块。这效果非常好。然而,我不确定这是否是正确的 Drupal 策略,我很乐意收到经验丰富的 Drupal 开发人员的一些反馈。

I have a Drupal 6 website with about 20 pages. Inside every page, I need to create a lot of widgets with information either stored inside the database or from external web services. Most of the time, a "view" (from the view module) is just not enough to solve the requirement.

Up until now, any time I need such a widget, I create a new module which implements hook_block. Then, I drag and drop this new module inside the panel I want. I will need to create about 20 modules. This works pretty good. However, I'm not sure if this is the correct-drupal-strategy and I would love to receive some feedback from experienced Drupal developers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2024-10-29 06:18:57

一个模块可以公开任意数量的块(理论上,admin/build/blocks 会教你否则;))。

查看 hook_block( 的文档),你只需要扩展你的返回多个块信息,然后根据 $delta 决定显示哪一个。

因此,您不需要 20 个单独的模块(也许是 2-3 个)并将这些块以某种方式分组在一起,因为只有一个模块可能很难维护。问题是,每个模块都会使您的网站速度稍微慢一些(至少还要加载一个文件, module_implements() 需要为每个钩子循环遍历每个模块,依此类推)。

如果没有更多信息,很难给出更好的建议。也许您可以将数据公开给视图,或者编写一个视图插件以您想要的方式显示它,或者......

A module can expose as many blocks as you want (in theory, admin/build/blocks will teach you otherwise ;)).

Have a look at the documentation of hook_block(), you just need to extend yours to return multiple block infos and then decided which one to show based on the $delta.

So you don't need 20 separate modules, maybe 2-3 and group the blocks somehow together because just a single module might be hard to maintain. The thing is that every single module makes your site a tiny bit slower (at least one more file to load, module_implements() needs to loop over every module for every hook and so on).

Without more information , it's hard to give any better advice. Maybe you could expose your data to views, or write a views plugin to display it in the way you want it, or...

甜心小果奶 2024-10-29 06:18:57

尽管 Berdir 的回答非常好,但令我印象深刻的是其中没有任何文档的链接。 hook_block 适用于多个块,并且他们可以共享构建其内容的功能。 API 页面很好,它给出的示例同时定义了两个块。

您应该注意到每个定义的块都有一个增量($blocks 数组中的键)。您可以拥有动态增量并使用其中的值来获取数据(例如,传递 nid 或 uid 并获取相关内容)。

Although Berdir's answer is pretty good, I'm impressed there's no link to any documentation in it. hook_block is meant for several blocks, and they can share functions that build their content. The API page is good, the example it gives defines two blocks at once.

You should notice each defined block has a delta (a key in the $blocks array). You can have dynamic deltas and use values in it to fetch data (passing a nid or uid and getting related content, for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文