Home Web Front-end HTML Tutorial Drupal 7 module development Create module First page (hook_menu)_html/css_WEB-ITnose

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

Jun 24, 2016 am 11:50 AM

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)

    ? ?

    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

    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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

    To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

    How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

    The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

    HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    See all articles