从我的模块定义的菜单返回 404 错误
我是 Drupal 的新手。我使用以下代码创建了一个 contace1 模块:
contace1.info
; $Id$
name = Contact1
description = Show how to build contact form
package = Pro Drupal Development
core = 6.x
contact1.module
// $Id$
/**
* @file
* practice to build form
*/
/**
* Implimentation of hook_menue().
*/
function contact_menu()
{
$items['contact1'] = array(
'title' => 'Contact',
'page callback' => 'contact_page',
'access argument' => array('access content'),
'type'=>MENU_CALL_BACK,
'access callback' => TRUE,
);
return $items;
}
/**
* menu callback
* called when user goes to http://localhost/drupaldemo/?q=contact
*/
function contact_page()
{
$output = t('You can leave a message using the contact form below.');
//Return the html generated from $form data structure.
$output.= drupal_get_form('contact_nameform');
return $output;
}
/**
* define the form
*/
function contact_nameform()
{
$form['user_name']= array(
'#title' =>t('Your Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
) ;
return $form;
}
/**
* validate the form
**/
function contact_nameform_validate()
{
if($form_state['values']['user_name']=="")
{
form_set_error('user_name',t('Please enter your name.'));
}
}
/**
* handle post validation form submition
*/
function contact_nameform_submit($form ,&$form_state)
{
$name=$form_state['values']['user_name'];
drupal_set_message(t('Thanks for filling out the form, %name',array('%name'=>$name)));
}
在这段代码中我尝试创建新的联系表单,
但它没有显示任何链接,并且在打开页面上直接给出找不到页面。
I am new to Drupal. I have created a contace1 module with the following code:
contace1.info
; $Id$
name = Contact1
description = Show how to build contact form
package = Pro Drupal Development
core = 6.x
contact1.module
// $Id$
/**
* @file
* practice to build form
*/
/**
* Implimentation of hook_menue().
*/
function contact_menu()
{
$items['contact1'] = array(
'title' => 'Contact',
'page callback' => 'contact_page',
'access argument' => array('access content'),
'type'=>MENU_CALL_BACK,
'access callback' => TRUE,
);
return $items;
}
/**
* menu callback
* called when user goes to http://localhost/drupaldemo/?q=contact
*/
function contact_page()
{
$output = t('You can leave a message using the contact form below.');
//Return the html generated from $form data structure.
$output.= drupal_get_form('contact_nameform');
return $output;
}
/**
* define the form
*/
function contact_nameform()
{
$form['user_name']= array(
'#title' =>t('Your Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
) ;
return $form;
}
/**
* validate the form
**/
function contact_nameform_validate()
{
if($form_state['values']['user_name']=="")
{
form_set_error('user_name',t('Please enter your name.'));
}
}
/**
* handle post validation form submition
*/
function contact_nameform_submit($form ,&$form_state)
{
$name=$form_state['values']['user_name'];
drupal_set_message(t('Thanks for filling out the form, %name',array('%name'=>$name)));
}
im this code i have tried to create new contact form
but it does not show any link and on opening page directly give page not found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您好,尝试使用此代码
在这里,我设置 markup 类型以返回包含内容和表单的输出,还设置 contact_nameform($form, $form_state) 的参数
Hi try to use this code
Here,I set markup type to return output with content and form, also set the parameter of contact_nameform($form, $form_state)
代码中的第一个错误是,如果模块名为 contact1.module,那么它实现的每个钩子都应该有一个以 contact1_ 开头的名称。然后,您应该避免在模块函数名称中使用 contact_,因为 Drupal 6 中已经存在 Contact 模块;如果您的模块适用于 Drupal 6,则模块之间会存在冲突。
第二个错误是您使用的常量是
MENU_CALLBACK
,而不是MENU_CALL_BACK
。如果 contact1.module 是你的模块的名称,那么它附带的信息文件应该命名为 contact1.info,而不是 contace1.info。如果您对该文件使用了错误的名称,Drupal 6 及更高版本不应在您可以安装的模块列表中显示您的模块。
The first error in the code is that, if the module is named contact1.module, then every hook it implements should have a name starting with contact1_. You should then avoid using contact_ in the name of your module's functions, as there is already the Contact module in Drupal 6; in the case your module is for Drupal 6, there would be a conflict between the modules.
The second error is that the constant you are using is
MENU_CALLBACK
, notMENU_CALL_BACK
.If then contact1.module is the name of your module, the info file that comes with it should be named contact1.info, not contace1.info. If you use a wrong name for that file, Drupal 6 and higher should not show your module in the list of the modules you can install.
'type' = MENU_CALL_BACK - 菜单是回调,您应该将其设置为 MENU_NORMAL_ITEM 或在管理页面中手动创建菜单到 contact1 页面。刷新缓存。
我建议您充分阅读 Vandyk 的“Pro Drupal Development”,其中有如何创建表单的示例:)
'type' = MENU_CALL_BACK - menu is callback, you should set it to MENU_NORMAL_ITEM or manually create menu in admin page to contact1 page. Refresh cache.
I recommend to you fully read "Pro Drupal Development" from Vandyk, there's examples how to create forms :)
首先,Drupal 中没有定义
MENU_CALL_BACK
。您想要编写的是MENU_CALLBACK
,它在菜单路由器中注册一个菜单项。该项目通常不会出现在任何可见的菜单中。将其视为隐藏的菜单项。如果您想让其可见,请使用MENU_NORMAL_ITEM
。First of all,
MENU_CALL_BACK
does not defined in Drupal. What you wanted to write isMENU_CALLBACK
, which registers a menu item in the menu router. This item won't appear in any visible menu normally. Think of it as a hidden menu item. If you want to make it visible, useMENU_NORMAL_ITEM
.