search
HomeWeb Front-endHTML TutorialDrupal 7 module development Create module First page (hook_menu)_html/css_WEB-ITnose

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

  • $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;}


    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)

    ? ?

    Statement
    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
    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

    The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    What is the viewport meta tag? Why is it important for responsive design?What is the viewport meta tag? Why is it important for responsive design?Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools