我使用 Field API 创建了以下字段(作为示例),效果很好。由于我想添加自动完成功能(已经工作,此处未显示)以及从变量设置默认值$_POST
,我开始使用hook_form_alter
.
更改字段就像一个魅力,但该字段将不再保存到节点,甚至出现在节点编辑表单的不同位置。
<?php
function trian_portal_enable() {
// create assigned License field
if (!field_info_field('field_assigned_license')){
$field = array(
'field_name' => 'field_assigned_license',
'type' => 'text',
'cardinality' => 1,
);
field_create_field($field);
$instance = array(
'field_name' => 'field_assigned_license',
'entity_type' => 'node',
'label' => t('Assigned License'),
'bundle' => 'kunden_download',
'description' => t('Enter License assigned to this download'),
'required' => FALSE,
'settings' => array(
// Here you inform either or not you want this field showing up on the user profile edit form.
'kunden_download_node_form' => 1,
),
'widget' => array(
'type' => 'textfield',
),
);
field_create_instance($instance);
}
}
function trian_portal_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'kunden_download_node_form') {
$form['field_assigned_license'] = array(
'#title' => t('Assigned Licence'),
'#type' => 'textfield',
'#default_value' => ($_REQUEST['lid']) ? $_REQUEST['lid']: '',
'#required' => ($_REQUEST['lid']) ? 1:0,
);
}
}
?>