`
haohappy2
  • 浏览: 315297 次
  • 性别: 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
分享到:
评论

相关推荐

    Beginning Drupal 7

    What you'll learn What Drupal is and why you should use it How to install a basic Drupal web site from scratch How to create content in your new Drupal web site How to manage users on your new Drupal...

    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 ...

    Beginning.Drupal.8.B00UH97G24

    Beginning Drupal 8 teaches you how to build, maintain, and manage Drupal 8-based web sites. The book covers what Drupal is, using Drupal when building a new web site, installing and configuring Drupal...

    Beginning Drupal 8(Apress,2015)

    Beginning Drupal 8 teaches you how to build, maintain, and manage Drupal 8-based web sites. The book covers what Drupal is, using Drupal when building a new web site, installing and configuring Drupal...

    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, ...

    Enterprise Drupal 8 Development

    In addition to a thorough discussion of custom module development and how to develop modules as building blocks, you'll also review many common ways of integrating Drupal with other 3rd party systems...

    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.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 for Absolute Beginners(Apress,2015)

    This book teaches you the basics of HTML, CSS, JavaScript, and PHP in relation to Drupal, so that you can begin to use this popular CMS with all of its features. You will first learn how to set up ...

    Drupal 6 JavaScript and JQuery

    With it's project-based approach, this book is carefully constructed to guide you from how JavaScript fits into the overall Drupal architecture through to making you a master of the jQuery library in ...

    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 ...

    Programmers.Guide.to.Drupal.Principles.Practices.and.Pitfalls.2nd.Edition

    Discover what’s new in Drupal 8 and learn how to take advantage of the powerful Drupal API. Get a complete overview of Drupal, including Drupal core and add-on modules and themes Learn Drupal’s ...

    drupal的addtoany模块

    drupal的addtoany模块,支持drupal6,可以方便的订阅、收藏或者通过email发送给其他朋友们,是个推广的好帮手。

    [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 ...

    Drupal 6 Panels Cookbook

    This book comes to your rescue and discusses all that you need to successfully incorporate Panels in your Drupal CMS. You will learn everything from setting up Panels, through using it, to integrating...

    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,...

Global site tag (gtag.js) - Google Analytics