- Drupal 8 Module Development
- Daniel Sipos
- 470字
- 2021-07-02 15:45:09
Hooks, plugins, and events
Now that we have seen what the core libraries, modules, and themes do, let's talk briefly about hooks and events to understand how they are all connected.
Hooks are a very typical Drupal procedural concept that allows Drupal core and modules to basically ask for data from other modules and themes (or expose it). By doing this, the latter can provide a new functionality or alter the existing ones. It is the responsibility of the code that calls the hook to make use of whatever the hook implementations return. The format and interface for what the latter need to return is usually documented in the hook documentation.
Concretely, hooks work by scanning installed modules and themes and looking for a function that follows a specific naming pattern (in other words, a hook implementation). This is, in most cases, in the following format--module_name_hook_name. Additionally, there are also alter hooks, which have the word alter tacked on the end of the function name and are used to change data passed as a reference to the hook implementation. We will see examples of hooks later in the book.
In previous versions of Drupal, hooks were KING. Yes, I wrote this with capital letters, my Caps Lock did not get stuck. This is because they were the way to add or extend a functionality in modules. As such, they were the single most important aspect of Drupal programming. In Drupal 8, however, although still important, they took a backseat to new concepts, such as plugins and events.
In Drupal 8, I dare to say that plugins are king. Much of the functionalities that used to be tied to Drupal via hooks is now added in through another Drupal typical concept--plugins (not to be confused with WordPress plugins). Drupal 8 plugins are discoverable bits of the functionality centralized by a manager and that are used for certain tasks and features. We will see more about plugins and provide many examples later in the book.
A third extension point introduced in Drupal 8 is the event system. Unlike the first two, however, this is not specific to Drupal, but is, in fact, the actual Symfony EventDispatcher component (http://symfony.com/doc/current/components/event_dispatcher.html). Events are primarily used in Drupal to intercept certain actions or flows in order to either stop or modify them. Many request to response tasks that were handled via hooks in the past are now being handled by dispatching events to check whether any modules are interested in, for example, delivering the response to the user.
- C程序設(shè)計簡明教程(第二版)
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- 小創(chuàng)客玩轉(zhuǎn)圖形化編程
- 軟件界面交互設(shè)計基礎(chǔ)
- 軟件測試項目實戰(zhàn)之性能測試篇
- JavaScript動態(tài)網(wǎng)頁開發(fā)詳解
- Jupyter數(shù)據(jù)科學(xué)實戰(zhàn)
- Oracle從入門到精通(第5版)
- 學(xué)習(xí)正則表達(dá)式
- Android Wear Projects
- Python項目實戰(zhàn)從入門到精通
- Test-Driven JavaScript Development
- CRYENGINE Game Development Blueprints
- Kotlin極簡教程
- Unity 5.X從入門到精通