继续Drupal分享精神 群已满 站长QQ:532116351【drupal交流+drupal 建站+theme制作】
登录 注册

drupal hook_form API 实例应用,form表单系统应用

今天自己操作学习了form简单的一些知识,在官方网上可以查询到很多form函数的一些类型介绍,有一个表格介绍的很详细, http://api.drupal.org/api/file/developer/topics/forms_api_reference.html 。而且都附有具体的例子讲解,满足基本的操作了。

下面就是我自己定义的产品表单,前面我自己定义了一个数据库product,额外的添加了几个字段,比如产地,外国名词。我做的是一个木地板企业的网站。
添加字段也可以用现有的CCk模块来做。但是我这里主要是自己来写,为了练习!

<?php
function product_form(&$node,$form_state) {


   
$type = node_get_types('type', $node);

   
$form['title'] = array(
       
'#type' => 'textfield',
       
'#title' => check_plain($type->title_label),
       
'#default_value' => $node->title,
       
'#required' => TRUE,
       
'#weight' => -8,
       
'#maxlength' => 255,
       
       
    );
   
$form['body']= array(
       
'#type' => 'textarea',
       
'#title' => check_plain($type->body_label),
       
'#default_value' => $node->body,
       
'#required' => TRUE,
       
'#rows' => 10,
       
'#weight' => -3,
       
    );
    
   
$form['origin'] = array(
       
'#type' => 'select',
       
'#title' => t('产地'),
       
'#default_value' => $node->origin,
       
'#options' => array(
        
1 => '欧洲',
        
2 => '非洲',
        
3 => '南美洲',
        
4 => '东南亚',
        
5 => '巴西',
        
6 => '印尼',
         ),
        
'#weight' => -7,

    );
   
$form['engname'] = array(
       
'#type' => 'textfield',
       
'#title' => t('国外名称'),
       
'#default_value' => $node->engname,
       
'#weight' => -6,
       
'#maxlength' => 255,
       
'#required' => TRUE
        
   
);

   
$form['laname'] = array(
       
'#type' => 'textfield',
       
'#title' => t('拉丁文'),
       
'#default_value' => $node->laname,
       
'#weight' => -5,
       
'#maxlength' => 255,
    );

   
$form['anothername'] = array(
       
'#type' => 'textfield',
       
'#title' => t('俗称'),
       
'#default_value' => $node->anothername,
       
'#weight' => -4,
       
'#maxlength' => 10,
    );

    return
$form;

   
}
?>

注意前面在具体的属性前叫 # 号,中间我就出现了Cannot use string offset as an array in 这样错误。找了半天才找出来。
'#required' 等于true,是必须的,那在添加内容时就必须在这个表单输入内容在容许提交,不然会自动报告错误。
可以用#weight 的数字来控制各个表单类型排列顺序!
select 类型就是下拉框选择,可以直接定义#default_value 默认值。drupal 真的让开发简单了很多,很多东西封装好了。

好了,保持后就可以看到了结果了。

评论

form功能确实很强大,durpal写的很灵活!

form功能确实很强大,durpal写的很灵活!

这个form表单写的很详细,不过要是能在加上 文件上传

这个form表单写的很详细,不过要是能在加上 文件上传 files 就更好啦,期待!

你用的编辑器是什麽??

你用的编辑器是什麽??

你说的是程序编辑器?还是网站编辑器 程序:editplus

你说的是程序编辑器?还是网站编辑器
程序:editplus
网站:默认的,drupal + wysiwyg + tinymce

发表新评论

  • 自动将网址与电子邮件地址转变为链接。
  • 允许HTML标签:<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • 自动断行和分段。
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

更多关於格式化选项的信息

CAPTCHA
请输入正确答案
Image CAPTCHA
Enter the characters (without spaces) shown in the image.