To create a module, please refer to "Drupal 7 Module Development and Creation"
If you want to support Chinese, the file format must be saved as UTF-8, NO BOM
---- --------------------------
hook_menu defines menu items and page feedback.
We need to replace the hook with our own module name. Here we create a my_first_module_menu() in the my_first_module.module file
<?phpfunction my_first_module_menu() { $items = array(); $items['mypage'] = array( 'title' => '第一个模块 ?? 页面', //菜单项的名称 'description' => '我的第一个模块页面', //当鼠标移动到菜单项,显示菜单的说明 'page callback' => 'my_page_view', //产生页面内容 'access callback' => TRUE, //所有人都能访问 'type' => MENU_NORMAL_ITEM, //菜单项显示类型 ); return $items;}function my_page_view() { $output = t('这是模块做的第一个页面。'); return $output;}
Note: Do not write ?>
at the end, clear the Cache, and then visit http://www.mysite. com/?q=mypage, you will see the following picture
(In fact, when you visit any http://www.mysite.com/?q=mypage/aaa/bbb, you will visit this page )
$items[] Path
Each $items corresponds to a path, written in [ ]. When the items use the same path, write the $items after the call. (Some articles say it is related to the parameter weight in $items. I tested that weight will not affect the order)
[ ] You can also use wildcards, for example: [node/%/edit] . For details, see: Wildcards in Pathssection in function hook_menu
title Required
Untranslated theme
title callback
Function that generates the theme. The default is t(). If you don't want translation, set FALSE (so we don't have to write 'title' => t('first form'))
title arguments
Parameters passed to t() or your custom function. Can be combined with path component
Reference: Menu item title and description callbacks, localization
description
Untranslated description.
page callback
When the user accesses the page path, call a function that displays the web page.
If not written, the callback function of the parent menu will be executed instead. In other words, $items['mypage'] and $items['mypage/child'] will execute the same content.
There is a special callback function drupal_get_form(), which will be developed later in Drupal 7 module to create, verify, and submit the form (Form). Specifically
page arguments
A string of parameter arrays passed to the page callback function. The above example is not used. Let’s modify it slightly:
After modification, the complete code is as follows:
<?phpfunction my_first_module_menu() { $items = array(); $items['mypage/%/edit'] = array( //修改路径 'title' => '第一个模块 ?? 页面', 'description' => '我的第一个模块页面', 'page callback' => 'my_page_view', 'page arguments' => array('hello', 1), //添加参数 'access callback' => TRUE, 'type' => MENU_NORMAL_ITEM, ); return $items;}function my_page_view($arg1, $arg2) { //添加参数传递 $output = t('这是模块做的第一个页面。'); $output .= '<br />' . $arg1 . ' ' . $arg2; //打印参数 return $output;}
Clear the Cache and then visit http://www.mysite.com/?q=mypage/world/edit. You will see the following picture
access callback
If you have permission to access this page, it must return TRUE, otherwise it will return FALSE (if not written, the default is FALSE). If false, the Access denied prompt will appear on the page.
type
Code describing the menu item properties. Many shortcut code constants are in menu.inc.
We use the default item MENU_NORMAL_ITEM, so you don’t need to write 'type' in $items => MENU_NORMAL_ITEM,
A few commonly used ones:
If you want to know more, you can open the /modules/menu/menu.modules file in the directory. Then combine it with http://www.yoursite.com/?q=admin/structure/menu/manage/navigation to help you understand
menu_name
If you don’t want to put this menu item under Navigation, you can specify it here. For example: 'menu_name' => 'main-menu', which will appear on the main menu together with Home
(more parameters will be improved slowly)
? ?