- Drupal 8 Module Development
- Daniel Sipos
- 684字
- 2021-07-02 15:45:13
Your first hook implementation
The module as it stands doesn't do much. In fact, it does nothing. However, do pat yourself on the back, as you have created your first Drupal 8 module. Before we move on to the interesting stuff we planned out, let's implement our first hook responsible for providing some helpful info about our module.
As we hinted at in the first chapter, when Drupal encounters an event for which there is a hook (and there are hundreds of such events), it will look through all of the modules for matching hook implementations. Now, how does it find the matching implementations? It looks for the functions that are named in the module_name_hook_name format, where hook_name is replaced by the name of the hook being implemented. The name of a hook is whatever comes after hook_. We will see an example below when we implement hook_help(). However, once it finds the implementations, it will then execute each of them, one after another. Once all hook implementations have been executed, Drupal will continue its processing.
Depending on the module size, it's recommended that you place all your hook implementations inside a .module file. There will be cases, however, when you'll organize them in other files, either by including those files inside the .module file yourself or using specific file naming conventions that gets them included by Drupal. However, for now, we stick with the default.
So, let's create a .module file in our module folder called hello_world.module and place an opening PHP tag at the top. In previous versions of Drupal, a .module PHP file was also required to get started with, but in Drupal 8, it is no longer necessary. That being said, we will create one now. Then, we can have the following hook_help() implementation inside (and typically all other hook implementations):
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function hello_world_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.hello_world':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This is an example module.') . '</p>';
return $output;
default:
}
}
As you can see, the name of the function respects the above-mentioned format--module_name_hook_name--because we are implementing hook_help. So, we replaced hook with the module name and hook_name with help. Moreover, this particular hook takes two parameters that we can use inside it; though, in our case, we only use one, that is, the route name.
The purpose of this hook is to provide Drupal some help text about what this module does. You won't always implement this hook, but it's good to be aware of it. The way it works is that each new module receives its own route inside the main p module, where users can browse this info--ours is help.page.hello_world. So, in this implementation, we will tell Drupal (and more, specifically, the core Help module) the following--if a user is looking at our module's help route (page), show the info contained in the $output variable. And that's pretty much it.
According to the Drupal coding standards, the DocBlock message above the hook implementation needs to stay short and concise, as in the preceding example. We do not generally document anything further for Drupal core hooks or popular contrib module hooks because they should be documented elsewhere. If, however, you are implementing a custom hook defined in one of your modules, it's okay to add a second paragraph describing what it does.
Users can now reach this page from the module administration page by clicking on the Help link for each individual module that has this hook implemented. Easy, right?

Even though we are not really providing any useful info through this hook, implementing it made helped us understand how a hook is implemented and what the naming convention is for doing so. Additionally, we saw an example of a traditional (procedural) Drupal extension point that module developers can use. In doing so, we literally extended the capability of the Help module by allowing it to give more info to users.
Now, let's move on to creating something of ours own.
- jQuery Mobile Web Development Essentials(Third Edition)
- Android和PHP開發最佳實踐(第2版)
- 華為HMS生態與應用開發實戰
- Vue.js快跑:構建觸手可及的高性能Web應用
- 區塊鏈架構與實現:Cosmos詳解
- FFmpeg入門詳解:音視頻流媒體播放器原理及應用
- Scratch 3游戲與人工智能編程完全自學教程
- Internet of Things with Intel Galileo
- Spring實戰(第5版)
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Kinect for Windows SDK Programming Guide
- 程序設計基礎教程:C語言
- Cocos2d-x Game Development Blueprints
- Django 3.0應用開發詳解
- 智能手機故障檢測與維修從入門到精通