Drupal 7模块无法正常工作

时间:2013-07-22 12:49:49

标签: drupal-7

全部,我遇到了新模块开发的问题。作为一个菜鸟我需要你的帮助。

Info file
; $Id$
name="cities"
description="List of cities"
core=7.x

Module file

 <?php
    // $Id$

    /**
    *impliments hook_menu()
    */

    /**
    * @file
    * cities module
    .
    */



 function cities_menu()
    {


 $items=array();
    $items['cities']=array(
    'title' => 'Cities listed',
    'page callbacks' => 'cities_page',
    'access arguments'=>array('view content')
    );
    return $items;
    }

    /**
    *Build a from containing :
    *(1)Space to input volume
    *(2)A Submit ("Convert") button.
    */


    function cities_form($form,&$form_state){
$form['cities']=array(
'#type'=>'select',
'#title' => t('cities'),
'#size'=>5,
'#default_value' => array('City list' => 'City list'),
'#options' => array('Kolkata' => 'Kolkata', 'Mumbai' => 'Mumbai','Cnennai' => 'Cnennai', 'Srilanka' => 'Srilanka','Delhi' => 'Delhi', 'Siliguri' => 'Siliguri'),
'#description'=>t('The number of Cities listed here'),
);

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

return $form;
    }

    function cities_page($form,&$form_state)
         {


return cities_form('cities_form');

         }
    function cities_form_submit($form,&$form_state)
        {


$output=(
    $from_state['values']['default_value']);

    set_drupal_message($output);
         }
    ?>

<?php // $Id$ /** *impliments hook_menu() */ /** * @file * cities module . */ function cities_menu() { $items=array(); $items['cities']=array( 'title' => 'Cities listed', 'page callbacks' => 'cities_page', 'access arguments'=>array('view content') ); return $items; } /** *Build a from containing : *(1)Space to input volume *(2)A Submit ("Convert") button. */ function cities_form($form,&$form_state){ $form['cities']=array( '#type'=>'select', '#title' => t('cities'), '#size'=>5, '#default_value' => array('City list' => 'City list'), '#options' => array('Kolkata' => 'Kolkata', 'Mumbai' => 'Mumbai','Cnennai' => 'Cnennai', 'Srilanka' => 'Srilanka','Delhi' => 'Delhi', 'Siliguri' => 'Siliguri'), '#description'=>t('The number of Cities listed here'), ); $form['submit']=array( '#type'=>'submit', '#value'=>t('Get Val'), ); return $form; } function cities_page($form,&$form_state) { return cities_form('cities_form'); } function cities_form_submit($form,&$form_state) { $output=( $from_state['values']['default_value']); set_drupal_message($output); } ?> 。除了这些,我在控制台中找不到任何错误信息。

请帮我运行我的代码。

感谢。

2 个答案:

答案 0 :(得分:0)

首先猜测:

<?php
function cities_menu() {
    $items['cities']=array(
    'title' => 'Cities listed',
    'page callbacks' => 'drupal_get_form',
    'page arguments' => array('cities_form'),
    'access arguments'=>array('view content'),
    );
    return $items;
    }
}

您必须致电drupal_get_form才能正确呈现表单(因为我看到您在cities_form中使用了hook_form,但您没有从drupal_get_form致电cities_page。不需要它,因为drupal_get_form(必须用于渲染表单本身)也可以通过hook_menu调用。

因此,使用此hook_menu,您可以完全跳过cities_page函数。

答案 1 :(得分:0)

我的错误代码

<?php
// $Id$

/**
*implements hook_menu()
*/

/**
* @file
* cities module
.
*/

function cities_menu() {
    $items['cities']=array(
    'title' => 'Cities listed',
    'page callback' => 'cities_form',
    'page arguments' => array('cities_form'),
    'access arguments'=>array('access content'),
    'type' => MENU_CALLBACK,
    );
    return $items;
}

function cities_form() {
    return drupal_get_form('cities_test_form');
}

function cities_test_form($form,&$form_state){
$form['cities']=array(

'#type'=>'select',
'#title' => t('The number of Cities listed here'),

'#default_value' => array('City list' => 'City list'),
'#options' => array('Kolkata' => 'Kolkata', 'Mumbai' => 'Mumbai','Cnennai' => 'Cnennai', 'Srilanka' => 'Srilanka','Delhi' => 'Delhi', 'Siliguri' => 'Siliguri'),

);

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

return $form;
}

function cities_test_form_submit($form_state, $form_values)
{
    $output = $form_values['values']['cities'];
    drupal_set_message($output);
}

?>