如何在 Drupal 中扩展贡献的模块?

发布于 2024-08-08 22:21:31 字数 126 浏览 4 评论 0原文

如何在不修改现有模块的情况下扩展它?实际上它是一个贡献的模块,所以我不想破解它。但我想改变并添加某些功能。 我是 drupal 的新手,当我阅读有关它的教程时,我一次又一次地听到一件事 - 不要破解核心......我相信这同样适用于模块。

How can I extend an existing module without modifying it? Actually its a contributed module, so I don't want to hack it. But I want to alter and add certain features to it.
I'm new to drupal and as I read tutorials about it, I keep hearing one thing again and again - Don't hack the core... and I believe the same applies for modules too.

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

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

发布评论

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

评论(5

暖心男生 2024-08-15 22:21:31

如果可能的话,避免破解或分叉模块。破解模块会给您带来较少的“黑客核心”痛苦,而分叉模块(没有明显优越的架构和功能集)只会进一步混淆模块的水 - 使网站建设者更难以做出有效的决策关于使用什么。

最好的解决方案是:

  1. 使用模块提供的任何 API。有时,有些函数可以用作 API 调用,但不一定是这样的。
  2. 创建附加功能并向模块问题队列提交补丁,以便其他人可以从您的工作中受益,因此您可以毫不费力地从安全审核中受益。
  3. 并非总是如此,但通常您可以创建一个实现 hook_form_alter 的自定义模块,并添加一个提交处理程序,该处理程序返回您的模块进行处理。

Avoid hacking or forking the module if at all possible. Hacking the module gives you a lesser variety of the "hacking core" pain, and forking the module (without a distinctly superior architecture and feature set.) just muddies the module water further- making it even more difficult for sitebuilders to make an effective decision about what to use.

The best solutions are:

  1. Use any API provided by the module. Sometimes there are functions you can use as API calls that are not necessarily intended that way.
  2. Create the additional functionality and submit a patch to the module issue queue, so others may benefit from your work, and so you may benefit from security audits without much sweat.
  3. Not always, but often you can go far creating a custom module that implements hook_form_alter and adds a submit handler that leads back to your module for processing.
七七 2024-08-15 22:21:31

找到了一篇关于在 drupal 中扩展模块的非常好的帖子 。基本部分:

  • 如果模块使用您想要更改的页面回调代码,请使用 hook_menu_alter() 更改页面回调。

  • 如果模块实现了您想要更改的主题函数,请更改与 hook_theme_registry_alter() 关联的函数。或者,如果足以更改主题函数获取的变量,那么您可以为该主题函数实现预处理函数(例如为 theme_rdf_metadata() 实现 hook_preprocess_rdf_metadata(),并更改主题函数将获取的变量。

  • 如果模块使用 db_select() 执行 SQL 查询,并为查询分配标签,请使用 hook_query_alter() 更改执行的查询。

  • 如果模块实现了一个您不希望它被执行的钩子,您可以实现hook_module_implements_alter()来避免它被执行。

  • 如果模块实现了一个更改钩子(例如hook_page_alter()),并且您想要更改该钩子更改的内容,请实现相同的更改钩子,并确保它在该模块实现的更改钩子之后执行。

如果您要更改的函数不是钩子,则:

  • 检查该函数正在使用从其他模块实现的钩子。例如,node_save() 调用 hook_node_presave();如果我想更改节点的“已更改”属性,我不会修改 node_save(),而是实现 hook_node_presave() 来更改它。

  • 检查函数是否被钩子引用/使用;在这种情况下,您可以为该钩子做一些事情,正如我之前所描述的。

如果我到目前为止所说的任何内容都不适用,那么最好创建一个自定义模块,并使用其他模块的代码来创建它。我还会尝试询问现有模块的功能请求,希望该功能能够实现。
破解第三方模块从来都不是一个好主意,特别是因为该模块不再可能自动更新模块(通过更新管理器或 Drush)。

Found a very good post about extending a module in drupal. Essential parts:

  • If the module uses code for a page callback that you want to change, change the page callback with hook_menu_alter().

  • If the module implements a theme function you want to alter, change the function associated with hook_theme_registry_alter(). In alternative, if it is sufficient to change the variables the theme function gets, then you can implement the preprocess function for that theme function (e.g. hook_preprocess_rdf_metadata() for theme_rdf_metadata(), and change the variables that theme function will get.

  • If the module executes an SQL query using db_select(), and assigns a tag to the query, change the executed query with hook_query_alter().

  • If the module implements a hook you don't want it is executed, you can implement hook_module_implements_alter() to avoid it is executed.

  • If the module implements an alter hook (e.g. hook_page_alter()), and you want to change what that hook altered, implement the same alter hook, being sure it is executed after the one implemented from that module.

In the case the function you want to alter is not a hook, then:

  • Check that function is using hooks implemented from other module. For example, node_save() invokes hook_node_presave(); if I would want to change the "changed" property of the node, I don't hack node_save(), but I rather implement hook_node_presave() to alter it.

  • Check that function is referenced/used from a hook; in that case, you can do something for that hook, as I previously described.

If anything I said until now doesn't apply, then it is better to create a custom module, and use the code of the other module to create it. I would also try asking a feature request for the existing module, hoping the feature is implemented.
Hacking a third-party module is never a good idea, especially because automatic updates of the module (via the Update Manager, or Drush) would not anymore possible for that module.

じ违心 2024-08-15 22:21:31

它是什么模块?有些模块提供 API,您可以通过编写自己的模块来扩展这些 API。有大量模块通过构建 Views API 来“扩展”视图。如果您想要扩展的模块没有好的 API,您可以随时对其进行分叉以制作满足您需求的版本。也许它也会对其他人有帮助。

What module is it? Some modules provide APIs that you can extend by writing a module of your own. There are tons of modules that "extend" Views by building off of the Views API. If the module you want to extend doesn't have a good API, you can always fork it to make a version that meets your needs. Maybe it will help someone else too.

梦与时光遇 2024-08-15 22:21:31

即使我们需要使用某些功能,我们也可以通过以下方式来完成。
我们需要从模块复制该函数,然后可以将该函数粘贴到 template.php 文件中。您可以相应地对该功能进行更改。

Even if we need to use some functionality we can do it in the following manner.
We need to copy the function from the module and we can paster the function in your template.php file. Than you can make chanages on that function accordingly.

江湖正好 2024-08-15 22:21:31

要更改模块显示的形式,您只需创建一个实现 hook_form_alter()hook_form_FORM_ID_alter()。大多数时候,当您想要修改模块的行为方式时,您还需要更改它使用的设置形式(或任何其他形式)。

To change the form shown by the module, you can simply create a module that implements hook_form_alter() or hook_form_FORM_ID_alter(). Most of the times, when you want to modify how a module behave, you also need to change the settings form it uses (or any other form).

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