0

所有,我有一个新模块开发的问题。作为一个菜鸟,我需要你的帮助。

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);
         }
    ?>

But when I typed mysitename/cities in address bar it redirects me on localhost. 除了这些,我在控制台中找不到任何错误信息。

请帮我运行我的代码。

谢谢。

4

2 回答 2

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才能正确呈现表单(正如我看到您使用 hook_form in cities_form,但您不调用drupal_get_formfrom cities_page。您也不需要它,因为drupal_get_form(必须用于呈现表单本身)也可以通过hook_menu调用)。

因此,使用这个 hook_menu,您可以完全跳过 city_page 功能。

于 2013-07-22T13:11:38.583 回答
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);
}

?>

于 2013-07-23T09:35:54.247 回答