在Drupal7专业开发指南 第三版里看到了这么一段代码:
/** * Implements hook_validate(). * 数据校验 */ function job_post_validate($node) { // Enforce a minimum character count of 2 on company names. // 公司名至少2个字 // 测试一下und是个什么东西 print_r($node); form_set_error('job_post_company', t('只是测试一下.'), $limit_validation_errors = NULL); if(isset($node->job_post_company) && strlen($node->job_post_company['und'][0]['value']) < 2) { form_set_error('job_post_company', t('The company name is too short. It must be atleast 2 characters.'), $limit_validation_errors = NULL); } }
$node
->job_post_company[
'und'
][0][
'value'
])里面的und不太好理解,所以干脆打印出来看了看,发现是这么一堆东西:
stdClass Object ( [nid] => [vid] => [uid] => 7 [created] => 1372676228 [type] => job_post [language] => zh-hans [changed] => [title] => 招聘程序员 [additional_settings__active_tab] => [revision] => [log] => [name] => demo [date] => [status] => 1 [promote] => 1 [sticky] => 0 [submit] => 保存 [preview] => 预览 [body] => Array ( [und] => Array ( [0] => Array ( [summary] => [value] => 啊啊啊啊啊啊啊啊啊 [format] => filtered_html ) ) ) [job_post_company] => Array ( [und] => Array ( [0] => Array ( [value] => 帝都公司 ) ) ) [form_build_id] => form-JztE_Qw-WrP5bnVKgO8TkrjKC3Af8DWnb3uO0JdvhXA [form_token] => AYii6Gm9ObHt1HQaBKD0Fz0woRpDetFsUBcSgO97aeY [form_id] => job_post_node_form [comment] => 2 [menu] => Array ( [enabled] => 0 [mlid] => 0 [module] => menu [hidden] => 0 [has_children] => 0 [customized] => 0 [options] => Array ( ) [expanded] => 0 [parent_depth_limit] => 8 [link_title] => [description] => [parent] => main-menu:0 [weight] => 0 ) [path] => Array ( [alias] => [pid] => [source] => [language] => und ) [op] => 保存
und这个键就一目了然了。从单词上来讲,und似乎是undefined的缩写。有一篇帖子http://drupal.stackexchange.com/questions/25839/best-practice-for-drupal-7-language-key-for-und-in-hook-form-alter 写道不应该硬编码und,因为这个键并不一定是und,似乎根据语言变动。帖子里有个人说应该这么写
function my_module_form_alter(&$form, &$form_state, $form_id) { if ($form_id == "someID") { // 硬编码是不对的 $form['field_charity_author']['und'][0]['value']['#value'] = arg(3); // 应该这么写 $field_language = field_language('node', $node, 'field_charity_author'); $form['field_charity_author'][$field_language][0]['value']['#value'] = arg(3); } }
自己倒不是很明白,看了帖子也不太明白,模模糊糊地感觉这玩意儿是个败笔,就这样。