Drupal“您无权访问此页面。”

时间:2012-02-24 19:09:21

标签: php drupal drupal-7 drupal-modules

我刚刚开始讨论drupal,如果这是一个非常愚蠢的问题,请道歉。我编写了以下模块,但每次尝试访问它时,转到url(http:// localhost:8888 / drupal / doodil_viral_signup)我都会收到拒绝访问的消息。我已经尝试重建权限并禁用和重新启用模块,但它似乎不起作用。

<?php
// $Id$
/**
* @file
* A module to encourage users to sign up.
* This module allows users to sign up to register for the site, and invite their friends to do the same.
*/

/**
* Implements hook_help().
*/
function doodil_viral_signup_help($path, $arg) {
  if ($path == 'admin/help#first') {
    return t('This module allows users to sign up to register for the site, and invite their friends to do the same.');
  }
}

/**
* Implements hook_menu().
*/
function doodil_viral_signup_menu($may_cache = true) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'doodil_viral_signup',
      'title' => t('Doodil Signup'),
      'callback' => 'doodil_viral_signup_page',
      'access' => TRUE,
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}

function doodil_viral_signup_page() {
  return drupal_get_form('doodil_viral_signup_page_form');
}

function doodil_viral_signup_page_form() {
  // [input text] First Name
  $form['first_name'] = array(
    '#type' => 'textfield',
    '#title' => t('First Name'),
  );

  // [input text] Last Name
  $form['last_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name'),
  );

  // [input text] Email Address
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Email Address'),
  );

  // [input submit] Sign Me Up
  $form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Sign Me Up'),
  );
  return $form;
}

function doodil_viral_signup_page_form_submit($form_id, $form_values) {
  $message = 'You have submitted the following information <pre>'.print_r($form_values).'</pre>';
  drupal_set_message(t($message));
}

有谁能告诉我如何解决这个问题?

提前致谢!

4 个答案:

答案 0 :(得分:3)

除了loganfsmyth之外,执行hook_menu()向您建议的所有内容,它应该是这样的:

function doodil_viral_signup_menu() {
  $items = array();
  $items['doodil_viral_signup'] = array(
    'title' => 'Doodil Signup',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('doodil_viral_signup_page_form'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

删除doodil_viral_signup_page()功能。

修改

这只是在我的机器上进行了测试,它的工作非常完美。如果它在您的机器上不起作用,则问题不在此模块中。

function doodil_viral_signup_help($path, $arg) {
  if ($path == 'admin/help#first') {
    return t('This module allows users to sign up to register for the site,
    and invite their friends to do the same.');
  }
}

function doodil_viral_signup_menu() {
  $items = array();
  $items['doodil_viral_signup'] = array(
    'title' => 'Doodil Signup',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('doodil_viral_signup_page_form'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK
  );
  return $items;
}

function doodil_viral_signup_page_form($form, &$form_state) {
  $form['first_name'] = array(
    '#type' => 'textfield',
    '#title' => t('First Name')
  );
  $form['last_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name')
  );
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#title' => t('Email Address')
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Sign Me Up')
  );
  return $form;
}

function doodil_viral_signup_page_form_submit($form, $form_state) {
  $message = t('Your submitted information <pre>!info</pre>', array(
    '!info' => print_r($form_state['values'], TRUE)
  ));
  drupal_set_message($message);
}

答案 1 :(得分:1)

当你给出的上述代码中的给定($ form,&amp; $ form_state)时。 您可能会遇到此类错误:

--------------------------------------
warning: Missing argument 2 for 
--------------------------------------

函数中只能有一个参数。

function_name($form)

答案 2 :(得分:1)

我得到了同样的错误。

解决这个问题: 删除下划线:page_callback,page_arguments和access_callback。

它们应该是“页面回调”,“页面参数”,“访问回调”。 (没有下划线)

答案 3 :(得分:0)

这段代码全部采用Drupal 5使用的格式。你的标签是drupal 7,所以我假设你想要的是什么?

function doodil_viral_signup_menu() {
  $items = array();
  $items['doodil_viral_signup'] = array(
    'title' => 'Doodil Signup',
    'page callback' => 'doodil_viral_signup_page',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

表格应为

function doodil_viral_signup_page_form($form, &$form_state) {

提交应该是:

function doodil_viral_signup_page_form_submit($form, $form_state) {
  drupal_set_message(t('You have submitted the following information <pre>!info</pre>', array(
    '!info' => print_r($form_state['values'], TRUE),
  )));
}

使用<pre>是一种丑陋的调试方法。您应该下载devel模块并使用它的函数dsm($form_state['values']);