You are here

28 machine_name(机读名字)

admin 的头像
Submitted by admin on 星期五, 2015-06-19 09:44

作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com

    创建一个单行的文本字段,用来存储唯一的机读名字。对这个字段会进行验证,以确保它是唯一的,并且不包含非法字符。所有的非法字符将会通过JavaScript替换为有效字符。注意这个元素对中文的支持不是很友好。

 

    它还包含一个非标准的元素属性#machine_name,它是一个关联数组,可包含以下键existssourcelabelreplace_patternreplace。exists是一个回调函数,用来检查机读名字是否存在;source指的是表单元素的#array_parents包含的用户可读名字,可用作机读名字的源,默认为array('name')label,机读名字的标签文本,默认为“机读名字”;replace_pattern,正则表达式,用来匹配机读名字中不允许的字符,默认为'[^a-z0-9_]+'replace,在机读名字中,用来替换非法字符的字符,默认为'_'

 

示例代码,来自(content_type.inc):

 

$form['type'] = array(

'#type' => 'machine_name',

'#default_value' => $type->type,

'#maxlength' => 32,

'#disabled' => $type->locked,

'#machine_name' => array(

'exists' => 'node_type_load',

),

'#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the %node-add page, in which underscores will be converted into hyphens.', array(

'%node-add' => t('Add new content'),

)),

);

 

常用属性: #access、 #after_build、 #ajax、 #attributes、 #autocomplete_path (默认为FALSE)、 #default_value、 #description (默认为 '一个唯一的机读名字。只能包含小写的字母,数字和下划线。')、 #disabled、 #element_validate、 #field_prefix、 #field_suffix、 #maxlength (默认为64)、 #parents、 #post_render、 #prefix、 #pre_render、 #process、 #required (默认为TRUE)、 #size (默认为60)、 #states、 #suffix、 #theme (默认为'textfield')、 #theme_wrappers (默认为'form_element')、 #title (默认为'机读名字')、 #title_display #tree、 #type、 #weight

 


Drupal版本: