2012-08-16 140 views
1

我對自定義drupal 7模塊有些麻煩。請注意,這不是我的第一個模塊。這是我的hook_menu;Drupal 7自定義模塊給出403

function blog_contact_menu(){ 
    $items = array(); 
    $items["blog_contact/send_to_one"] = array(
    "page_callback"=>"single_blogger_contact", 
    "access_arguments"=>array("access blog_contact content"), 
    "type"=>MENU_CALLBACK 
    ); 
    return $items; 
} 

而這是我的燙髮功能;

function blog_contact_perm() { 
    return array("access blog_contact content"); 
} 

這應該工作,但是當我做一個ajax調用,它給403禁止。您無權查看bla bla。我的ajax調用是正確和簡單的,url是正確的,type是post。我沒有直接看到原因。

回答

4

屬性在他們,而不是下劃線的空間。 access_arguments實際上應該是access argumentspage_argumentspage arguments等:

function blog_contact_menu(){ 
    $items = array(); 
    $items["blog_contact/send_to_one"] = array(
    "title" => "Title", 
    "page callback"=>"single_blogger_contact", 
    "access arguments"=>array("access blog_contact content"), 
    "type"=>MENU_CALLBACK 
); 
    return $items; 
} 

還要注意的是title是一個必需的屬性。

除此之外,hook_permission()問題已經提到您的代碼是現貨。

+0

好抓!我甚至沒有注意到這一點。 – nmc 2012-08-16 17:34:04

0

由於您沒有在hook_menu實施中指定access_callback,所以它默認使用user_access函數,並檢查是否授予access blog_contact content權限。

function blog_contact_menu(){ 
    $items = array(); 
    $items["blog_contact/send_to_one"] = array(
    // As mentioned in Clive's answer, you should provide a title 
    "title" => "Your Title goes here", 
    "page callback"=>"single_blogger_contact", 
    // No "access callback" so uses user_access function by default 
    "access arguments"=>array("access blog_contact content"), 
    "type"=>MENU_CALLBACK 
    ); 

access blog_contact content不是Drupal的知道這樣user_access函數返回false這就是爲什麼你得到403次拒絕訪問權限。

如果你想告訴Drupal關於access blog_contact content的權限,那麼鉤子是hook_permission,而不是hook_perm

而且你的代碼應該更像:在菜單項路由器

function blog_contact_permission() { 
    return array(
    'access blog_contact content' => array(
     'title' => t('Access blog_contact content'), 
     'description' => t('Enter your description here.'), 
    ), 
); 
}