官术网_书友最值得收藏!

Finishing Touches: hook_help()

We now have a functioning module. However, there is one last thing that a good Drupal module should have. Modules should implement the hook_help() function to provide help text for module users.

Our module is not very complex. Our help hook won't be, either:

/**
* Implementation of hook_help()
*/
function goodreads_help($path, $arg) {
if ($path == 'admin/help#goodreads') {
$txt = 'The Goodreads module uses the !goodreads_url XML '
.'API to retrieve a list of books and display it as block '
.'content.';
$link = l('Goodreads.com', 'http://www.goodreads.com');
$replace = array(
'!goodreads_url' => $link
);
return '<p>'. t($txt, $replace) .'</p>';
}
}

The hook_help() function gets two parameters: $path, which contains a URI fragment indicating what help page was called, and $arg, which might contain extra information.

In a complex instance of hook_help(), you might use a switch statement on the $path, returning different help text for each possible path. For our module, only one path is likely to be passed in: the path to general help in the administration section. This path follows the convention admin/help#<module name>, where<module name> should be replaced with the name of the module (e.g. goodreads).

The function is straightforward: The help text is stored in the $txt variable. If our module required any additional setup or was linked to additional help, we would want to indicate this here. But in our case, all we really need is a basic description of what the module does.

We also want to insert a link back to Goodreads.com. To do this, we create the placeholder (!goodreads_url) in the $txt content, and then create the link ($link) by calling the l() function.

Since we are going to pass the $txt string and the link into the translation function, and let that function replace the placeholder with the link, we need to put $link inside an array—the $replace array.

Finally the help text is returned after being passed through the t() translation function.

This help text will now be accessible on the Administer | Help page:

Finishing Touches: hook_help()

That is all there is to creating help text.

It is good coding practice to include help text with your module. And the more complex a module is, the better the help text should be. In future chapters, we will start every module by implementing hook_help(). Often, though, we will keep text in the help function brief for the sake of keeping the book concise and readable.

In your own modules you may want to create more verbose help text. This is helpful to users who don't have access to the code (or to a book explaining how the module works in detail).

主站蜘蛛池模板: 四会市| 繁峙县| 荔浦县| 平阴县| 梁山县| 扎兰屯市| 嘉峪关市| 广东省| 长兴县| 尼勒克县| 安义县| 浙江省| 拉萨市| 阿坝县| 靖江市| 惠安县| 垫江县| 崇义县| 平顺县| 大冶市| 龙里县| 永济市| 礼泉县| 聂拉木县| 确山县| 宁国市| 儋州市| 广汉市| 天水市| 手游| 常熟市| 鄂托克前旗| 曲周县| 六安市| 普安县| 旬邑县| 镇远县| 务川| 洛浦县| 巧家县| 武宣县|