Drupal6.x菜单系统的例子

这几天一直为创建菜单发愁,今天在http://api.drupal.org看到关于菜单系统的例子挺不错。
原文链接:page_example.module

看代码:

<?php
// $Id: page_example.module,v 1.13 2007/10/17 19:38:36 litwol Exp $

/**
 * @file
 * This is an example outlining how a module can be used to display a
 * custom page at a given URL.
 */


/**
 * Implementation of hook_help().
 *
 * Throughout Drupal, hook_help() is used to display help text at the top of
 * pages. Some other parts of Drupal pages get explanatory text from these hooks
 * as well. We use it here to illustrate how to add help text to the pages your
 * module defines.
 */

function page_example_help($path, $arg) {
  switch ($path) {
    case 'foo':
      // Here is some help text for a custom page.
      return t('This sentence contains all the letters in the English alphabet.');
  }
}

/**
 * Implementation of hook_perm().
 *
 * Since the access to our new custom pages will be granted based on
 * special permissions, we need to define what those permissions are here.
 * This ensures that they are available to enable on the user role
 * administration pages.
 */

function page_example_perm() {
  return array('access foo', 'access baz');
}

/**
 * Implementation of hook_menu().
 *
 * You must implement hook_menu() to emit items to place in the main menu.
 * This is a required step for modules wishing to display their own pages,
 * because the process of creating the links also tells Drupal what
 * callback function to use for a given URL. The menu items returned
 * here provide this information to the menu system.
 *
 * With the below menu definitions, URLs will be interpreted as follows:
 *
 * If the user accesses <a href="http://example.com/?q=foo" title="http://example.com/?q=foo">http://example.com/?q=foo</a>, then the menu system
 * will first look for a menu item with that path. In this case it will
 * find a match, and execute page_example_foo().
 *
 * If the user accesses <a href="http://example.com/?q=bar" title="http://example.com/?q=bar">http://example.com/?q=bar</a>, no match will be found,
 * and a 404 page will be displayed.
 *
 * If the user accesses <a href="http://example.com/?q=bar/baz" title="http://example.com/?q=bar/baz">http://example.com/?q=bar/baz</a>, the menu system
 * will find a match and execute page_example_baz().
 *
 * If the user accesses <a href="http://example.com/?q=bar/baz/1/2" title="http://example.com/?q=bar/baz/1/2">http://example.com/?q=bar/baz/1/2</a>, the menu system
 * will first look for bar/baz/1/2. Not finding a match, it will look for
 * bar/baz/1/%. Again not finding a match, it will look for bar/baz/%/2. Yet
 * again not finding a match, it will look for bar/baz/%/%. This time it finds
 * a match, and so will execute page_example_baz(1, 2). Note the parameters
 * being passed; this is a very useful technique.
 */

function page_example_menu() {
  // This is the minimum information you can provide for a menu item.
  $items['foo'] = array(
    'title' => 'Foo',
    'page callback' => 'page_example_foo',
    'access arguments' => array('access foo'),
  );

  // By using the MENU_CALLBACK type, we can register the callback for this
  // path but not have the item show up in the menu; the admin is not allowed
  // to enable the item in the menu, either.
  //
  // Notice that the 'page arguments' is an array of numbers. These will be
  // replaced with the corresponding parts of the menu path. In this case a 0
  // would be replaced by 'bar', a 1 by 'baz', and like wise 2 and 3 will be
  // replaced by what ever the user provides. These will be passed as arguments
  // to the page_example_baz() function.
  $items['bar/baz/%/%'] = array(
    'title' => 'Baz',
    'page callback' => 'page_example_baz',
    'page arguments' => array(2, 3),
    'access arguments' => array('access baz'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * A simple page callback.
 *
 * Page callbacks are required to return the entire page. The content
 * is then usually output via a call to theme('page'), where the theme system
 * will then surround the content in the appropriate blocks, navigation, and
 * styling.
 *
 * If you do not want to use the theme system (for example for outputting an
 * image or XML), you should print the content yourself and not return anything.
 */

function page_example_foo() {
  return '<p>'. t('The quick brown fox jumps over the lazy dog.') .'</p>';
}

/**
 * A more complex page callback that takes arguments.
 *
 * The arguments are passed in from the page URL. The in our hook_menu
 * implementation we instructed the menu system to extract the last two
 * parameters of the path and pass them to this function as arguments.
 */

function page_example_baz($alice, $bob) {
  // Make sure you don't trust the URL to be safe! Always check for exploits.
  if (!is_numeric($alice) || !is_numeric($bob)) {
    // We will just show a standard "access denied" page in this case.
    return drupal_access_denied();
  }

  $list[] = t("Alice's number was @number.", array('@number' => $alice));
  $list[] = t("Bob's number was @number.", array('@number' => $bob));
  $list[] = t('The total was @number.', array('@number' => $alice + $bob));

  return theme('item_list', $list);
}

自己动手创建模块测试
在 sites\all\modules目录下新建文件夹并命名为:page_example
新建文件命名为:page_example.module ,并将上面的代码复制到page_example.module中保存
新建文件命名为:page_example.info
输入:

name = "page_example"
version = "6.x-1.0"
core = "6.x"

保存后,到 管理-->站点构建-->模块
就会在模块列表中看到page_example,启用并保存后,就会在导航菜单看"Foo"项,URL:/foo 会显示

Foo
This sentence contains all the letters in the English alphabet.

The quick brown fox jumps over the lazy dog.

URL到:/bar/baz/1/1 会显示

Baz

* Alice's number was 1.
* Bob's number was 1.
* The total was 2.

这个例子真不错,我还多研究一下代码

评论

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 允许HTML标签:<a> <img> <em> <del> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h5> <h6>
  • 自动断行和分段。
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.