【问题标题】:Drupal - Page not found for custom moduleDrupal - 找不到自定义模块的页面
【发布时间】:2014-04-03 08:32:22
【问题描述】:

我对 Drupal 还很陌生,刚刚制作了我的第一个模块,它显示了一个表单。在 hook_menu() 中,我设置了 $items['form'],如下所示。当我导航到 mysite.com/form 时,它没有显示任何内容,除了主题和:找不到页面,找不到请求的页面“/form”。

我试图清除我的缓存,给我的模块一个不同的名称,确保我的模块已启用,但没有任何帮助。有谁知道问题可能是什么? .module 文件中的代码就在这里:

<?php 

//implements hook_permission()
function form_example_permission() {
  return array(
    'Submit form_example' => array(
      'title' => t('Submit form_example'),
      'description' => t('Submit the form_example form'),
    ),
  );
}


//implements hook_menu()
function form_example_menu() {
  $items['form'] = array(
    'title' => 'My Example Form',
    'type' => MENU_NORMAL_ITEM,
    'acces' => TRUE,
    'page callback' => 'drupal_get_form()',
    'page arguments' => array('form_example_form'),
    'acces arguments' => array('acces content'),
  );

  return $items;
}

//implements hook_form()
function form_example_form($form, &$form_state) {
  $form['mynumber'] = array (
    '#type' => 'textfield',
    '#title' => t('My Number'),
    '#size' => 10,
    '#maxlength' => 10,
    '#required' => TRUE,
    '#description' => t('Please enter a valid number'),
  );

  $form['mytextfield'] = array(
    '#type' => 'textfield',
    '#title' => t('My Textfield'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
  );

  $form['mytext'] = array(
    '#title' => t('My Textarea'),
    '#type' => 'textarea',
    '#description' => t('Enter some text'),
    '#default value' => '',
    '#required' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add item'),
  );

  return $form;
}

【问题讨论】:

  • 感谢您的回答,但使用不带括号的 drupal_get_form 它仍然给我一个 404 错误
  • 评论太长了,所以我把它删除并作为答案。

标签: php forms drupal drupal-7


【解决方案1】:

'page callback' =&gt; 'drupal_get_form() 错误,需要去掉函数名中的括号。正确的实现将是page callback' =&gt; 'drupal_get_form。并且acces =&gt; TRUE也是错误的,它必须是'访问回调',如果你设置了固定的TRUE值那么你不需要指定access arguments键。因为access callback 无论如何都会返回 TRUE。进一步reading..

更新: 示例:

function form_example_permission() {
  return array(
    'submit form_example' => array(
      'title' => t('Submit form_example'),
      'description' => t('Submit the form_example form'),
    ),
  );
}


//implements hook_menu()
function form_example_menu() {
  $items['form'] = array(
    'title' => 'My Example Form',
    'type' => MENU_NORMAL_ITEM,
    'access callback' => 'user_access',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_example_form'),
    'access arguments' => array('submit form_example'),
  );

  return $items;
}

(您的代码中有错字)不要忘记刷新缓存。

【讨论】:

  • 我按照你的指示,我现在正在使用:'page callback' => 'drupal_get_form', 'page arguments' => array('form_example_form'), 'acces callback' => array( '访问内容'),仍然无法正常工作。在 hook_menu 之外还有其他可能吗?
  • @Elendas 如果你使用'acces arguments' =&gt; array('submit form_example') 然后将access callback =&gt; TRUE 更改为access callback =&gt; user_access
  • 我现在正在使用:function form_example_menu() { $items['example/form'] = array( 'title' => 'My Example Form', 'page callback' => 'drupal_get_form ', 'page arguments' => array('form_example_form'), 'acces callback' => 'user_access', 'acces arguments' => array('submit form_example'), 'type' => MENU_NORMAL_ITEM, );返回$项目; Wich 仍然不起作用,所以我怀疑它在 hook_menu 之外的某个地方。有什么建议吗?
【解决方案2】:

请将acces 的拼写更改为access 并将drupal_get_form() 更改为drupal_get_formclear a caches 之后

function form_example_menu() {
  $items['form'] = array(
    'title' => 'My Example Form',
    'type' => MENU_NORMAL_ITEM,
    'access' => TRUE,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('form_example_form'),
    'access arguments' => array('access content'),
  );

  return $items;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多