从我的模块定义的菜单返回 404 错误

发布于 2024-09-17 07:47:29 字数 2039 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-09-24 07:48:37

您好,尝试使用此代码

/**
* menu callback
*
*/

function contact_page()
    {
        $output = array(
            'item 1' => array(
              "#type" => 'markup',
              '#markup' =>  t('You can leave a message using the contact form below.'),
            ),
            'item 2' => array(
              "#type" => 'markup',
              '#markup' =>  drupal_get_form('contact_nameform'),
            ),
        );
        return $output;
    }
    /**
    * define the form
    */
function contact_nameform($form, $form_state)
    {  ..........
       .......

在这里,我设置 ma​​rkup 类型以返回包含内容和表单的输出,还设置 contact_nameform($form, $form_state) 的参数

Hi try to use this code

/**
* menu callback
*
*/

function contact_page()
    {
        $output = array(
            'item 1' => array(
              "#type" => 'markup',
              '#markup' =>  t('You can leave a message using the contact form below.'),
            ),
            'item 2' => array(
              "#type" => 'markup',
              '#markup' =>  drupal_get_form('contact_nameform'),
            ),
        );
        return $output;
    }
    /**
    * define the form
    */
function contact_nameform($form, $form_state)
    {  ..........
       .......

Here,I set markup type to return output with content and form, also set the parameter of contact_nameform($form, $form_state)

腹黑女流氓 2024-09-24 07:48:25

代码中的第一个错误是,如果模块名为 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, not MENU_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.

究竟谁懂我的在乎 2024-09-24 07:48:13

'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 :)

妄断弥空 2024-09-24 07:48:01

首先,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 is MENU_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, use MENU_NORMAL_ITEM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文