`
haohappy2
  • 浏览: 326269 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

how to manage menu in drupal

阅读更多
1.callback mapping 
2.visit permission 
3.menu

include/menu.inc
menu.module

http://www.example.com/?q=cats
http://www.example.com/?q=node/3 they are same

how to create mymenu

mymenu.info
;$Id$
name="Mymenu Module"
descrption="Adds a menu to the navigation block"
varsion="$Name$"

mymenu.module
<?php
//$Id$
function mymenu_menu($may_cache){
   $items=array();
   if($may_cache){
       $items[]= array(
           'title'=>t('Greeting'),
           'path'=>'mymenu',
           'callback'=>'mymenu_hello',
           'access'=>TRUE
       );
   }else{
      $timestamp=format_date(time(),'small');
      $items[]=array(
         'title'=>t('stock quote at @time',array('@time'=>$timestamp)),
         'path'=>'stockquote',
         'callback'=>'mymenu_stock_quote',
         'access'=>TRUE
      );
   }
   return $items;
}
function mymenu_hello(){
   return t('hello');
}
?>
/**
if you want to develop your own module. you should install devel.module
if you don't install it. you can  delete the table cache_menu by hand
truncate table 'cache_menu'
and now we can change the menu and then via  $websiteaddress?q=mymenu
it will show
     HELLO good looking
if we use $webaddress?q=mymenu/Fred
     HELLO Fred
*/
function mymenu_hello($name=NULL){
  if(!isset($name)){
     $name=t('good looking');
  }
  return t('HELLO @name!',array('@name'=>$name));
}
/**
  you can define callback mapping in the hook function.you can load the same callback mapping
  functional from differnt menu. and show the hidden infomation
*/
function mymenu_menu($may_cache){
  $item=array();
  if($may_cache){
       $items[]= array(
           'title'=>t('Greeting'),
           'path'=>'mymenu',
           'callback'=>'mymenu_hello',
           'callback arguments'=>array(t('Hi!'),t('Ho!)),
           'access'=>TRUE
        );
  }
}
function mymenu_hello($first,$second,$name=NULL){
  drupal_set_message(t('first is %first',array('%first'=>$first)));
    drupal_set_message(t('first is %second',array('%second'=>$second)));
  if(!isset($name)){
     $name=t('good looking');
  }
  return t('HELLO @name!',array('@name'=>$name));
}

/*how to add a sub_menu/
function mymenu_menu($may_cache){
  $item=array();
  if($may_cache){
       $items[]= array(
           'title'=>t('Greeting'),
           'path'=>'mymenu',
           'callback'=>'mymenu_hello',
           'callback arguments'=>array(t('Hi!'),t('Ho!)),
           'access'=>TRUE
       );
       $items[]= array(
           'title'=>t('Fareware'),
           'path'=>'mymenu/goodbye',
           'callback'=>'mymenu_goodbye',
           'access'=>TRUE
       );
      
  }
  return $items;
}
/**
All above, we set the acces as true. how to manage it. and hook_perm

*/
function mymenu_perm(){
  return array('receive greeting');
}

/*
  if we set above 'access'=>user_access('receive greeting'). it mean that we only allow
   receive greeting to visit this menu. in some conditions. we always meet. we don't need
   to create the menu. but we should  allocate the callback mapping
   we always  use  MENU_CALL_BACK,MENU_LOCAL_TASK MENU_DEFAUTLT_LOCAL_TASK
   we set node.module as a example
   At local, this always occur at node,user,workflow
*/
node.module
$items[]= array(
'path'=>'rss.xml',
'title'=>t("RSS Feed'),
'callback=>'node_feed',
'access'=>user_access('access content'),
'type'=>MENU_CALLBACK
);

/*drupal can set two level menu defaultm */

function bookstore_menu($may_cache){
   $items=array();
   $items[]= array(
           'title'=>t('Books'),
           'path'=>'bookstore',
           'callback'=>'bookstore_overview',
           'type'=>MENU_CALLBACK,
           'access'=>TRUE
   );
   $items[]= array(
           'title'=>t('Books->List'),
           'path'=>'bookstore/list',
           'callback'=>'bookstore_list',
           'type'=>MENU_DEFAULT_LOCL_CALLBACK,
           'access'=>user_access('list bookstore')
   );
   $items[]= array(
           'title'=>t('Books->ADD'),
           'path'=>'bookstore/ADD',
           'callback'=>'bookstore_add',
           'type'=>MENU_LOCL_CALLBACK,
           'access'=>user_access('add bookstore')
   );
   $items[]= array(
           'title'=>t('Books->List->shanghai'),
           'path'=>'bookstore/list/shanghai',
           'callback'=>'bookstore_list',
           'type'=>MENU_LOCL_CALLBACK,
           'access'=>user_access('list bookstore')
   );
   $items[]= array(
           'title'=>t('Books->List->BeiJing'),
           'path'=>'bookstore/list/beijing',
           'callback'=>'bookstore_list',
           'type'=>MENU_LOCL_CALLBACK,
           'access'=>user_access('list bookstore')
   );
   return $items;
}

function bookstore_overview(){
  $output=t('the following falvors are avaliable');
  return $output;
}

/** let use devel for a menu example
  use this way we don't need to change the core drupal code
*/
function mymodule_menu($may_cache){
   $items=array();
   if(!$may_cache&&module_exist('devel')){
   $items[]=array(
     'path'=>'devel/cache/clear',
     'title'=>t('wrap cache clear'),
     'callback'=>'mymodule_clear_cache',
     'type'=>MENU_CALLBACK,
     'access'=>user_access('access devel information')
     //same as devel.module
   );
   }
}

function mymodule_clear_cache(){
  drupal_set_message('we got called first');
  devel_cache_clear();
}
//if we want to delete a menu
$items[]= array(
  'path'=>'node/add',
  'title'=>t('this should not shown up'),
  'callback'=>'drupal_not_found',
  'type'=>MENU_CALLBACK
);
/**
  let do a interesting things. add a menu to delete all users in the exsit menu
   if you want to display the menu at two place  you can do like this
   'type' => MENU_NORMAL_ITEM | MENU_LOCAL_TASK
*/
$items[]= array(
  'path'=>'admin/user/deleteall',
  'title'=>t('delete all users'),
  'callback'=>'mymodule_deleteall_users',
  'type'=>MENU_LOCAL_TASK,
  'access'=>user_access('delete all users')
);
0
1
分享到:
评论

相关推荐

    Decoupled Drupal in Practice

    You will learn how to architect and implement decoupled Drupal architectures across the stack―from building the back end and designing APIs to integrating with front-end technologies. You’ll also ...

    Drupal.8.for.Absolute.Beginners.1430264667

    The book targets anyone wishing to learn either basic web technology, Drupal, or both, and in particular it shows how basic web technologies fit into working with Drupal. Even if you know basic CSS, ...

    Drupal.8.Configuration.Management.1783985208

    Additionally, you will learn how to migrate configuration from Drupal 6 and 7 to Drupal 8 and how to manage configuration for multilingual websites. Table of Contents Chapter 1. Understanding ...

    Drupal.8.Development.Beginners.Guide.2nd.Edition.epub

    If you are a developer who wants to use Drupal to enhance your website project and web application to manage content, this book is for you. Whether you are new to Drupal or an experienced web ...

    Drupal 8 Explained: Your Step-by-Step Guide to Drupal 8

    Drupal 8 Explained: Your Step-by-Step Guide to Drupal 8 (The Explained Series) by Stephen Burge English | 3 Apr. 2017 | ASIN: B06Y1VN2D7 | 283 Pages | AZW3 | 13.66 MB We're delighted to present the ...

    Drupal 8 Module Development 2nd Edition

    In his spare time, he runs webomelette, a Drupal website where he writes technical articles, tips, and techniques related to Drupal development. Table of Contents Developing for Drupal 8 Creating ...

    drupal的addtoany模块

    Drupal的AddToAny模块是一个非常实用的工具,尤其对于那些希望增强网站社交分享功能的用户而言。这个模块专为Drupal 6设计,旨在提供一种简便的方式,让访客能够订阅、收藏网页内容,并且能够通过电子邮件轻松地将...

    [Drupal] Drupal 响应式主题 开发教程 (英文版)

    Ideal for experienced Drupal developers, this book takes you through RWD basics and shows you how to build sites based on Aurora, Zen, and Omega—three popular base themes created by Drupal ...

    the definitive guide to drupal 7

    - **第 29 章:菜单系统与进入 Drupal 的路径**(Chapter 29: The Menu System and the Path Into Drupal) - 介绍了 Drupal 的菜单系统及其与导航和内容组织的关系。 - **第 30 章:深入探究:Drupal 在显示页面时...

    drupal 6.12

    Drupal requires access to a database in order to be installed. Your database user will need sufficient privileges to run Drupal. Additional information about privileges, and instructions to create ...

    Learning Drupal 6 Module Development

    The new menu system, the Forms and Schema APIs, and many core revisions are covered in this book. What you will learn from this book? * A developer's overview of important Drupal concepts and APIs,...

    drupal-9.0.1_drupal9_drupal9教程_drupal9开发实例_

    Drupal 9.0.1是Drupal内容管理系统的一个重要版本,带来了许多新特性和改进,旨在提升网站构建者的体验和网站的性能。Drupal是一款开源的PHP框架,被广泛用于创建复杂、可扩展的Web应用程序和网站。这个9.0.1版本的...

    Drupal7宝典+Drupal开发指南+Using Drupal

    Drupal是开源的内容管理系统(CMS),在全球范围内被广泛用于构建各种网站,从个人博客到复杂的商业平台。本资源包包含了三本书籍,可以帮助你深入理解和掌握Drupal7的各个方面: 1. **Drupal7宝典**: 这本书全面...

Global site tag (gtag.js) - Google Analytics