Drupal 7 module development Create module First page (hook_menu)_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:50:35
Original
1183 people have browsed it

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;}
Copy after login

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

  • $items first defines the path in [ ], and when accessing the /mypage page, call my_page_view Function generated page
  • 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:

  • Modify it $items, the path becomes $items['mypage/%/edit'], which means when accessing mypage/1/edit, or mypage/2/edit, etc., execute this function
  • Add page arguments: 'page arguments' => array('hello', 1),
    'hello' is passed to $arg1 as a constant
    1 represents the first layer behind the path mypage: when you access mypage/1/edit, return It is 1; if you access mypage/world/edit, the return is world
  • Add parameters to function my_page_view, function my_page_view($arg1, $arg2)
  • 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;}
    Copy after login


    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:

  • MENU_NORMAL_ITEM, displayed under the Navigation menu list, the administrator can drag or hide
  • MENU_SUGGESTED_ITEM, the same as MENU_NORMAL_ITEM, but the default state is Disabled and needs to be managed The member can manually Enabled
  • MENU_CALLBACK will not generate menus and breadcrumbs. Just simply register a path and execute the corresponding function when the path is accessed. Usually called by API.
  • MENU_LOCAL_ACTION In the parent menu, display a link to guide the next step. For example: add a menu
  • MENU_LOCAL_TASK is also used as a link, but is usually displayed in tag (TAB) format
  • MENU_DEFAULT_LOCAL_TASK is the same as MENU_LOCAL_TASK Tag (TAB) is just the default tag, which is the same as the parent menu path (the path of this itmes only expresses the parent menu)
  • 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)

    ? ?

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template