[英]D7: Field API Field not saved when using hook_form_alter
I have the following field (as an example) created with Field API, which works great. 我使用Field API创建了以下字段(作为示例),效果很好。 As I want to add autocomplete functionality (already working, not shown here) as well as setting a default value from
$_POST
variable, I started altering the field with hook_form_alter
. 当我想添加自动完成功能(已经在工作,此处未显示)以及从
$_POST
变量设置默认值时 ,我开始使用hook_form_alter
更改该字段。
Altering the field works like a charm, BUT the field won't be saved anymore to the Node and even appears on a different place in the node edit form . 更改字段就像一个超级按钮一样, 但是该字段将不再保存到Node中,甚至会出现在node edit表单中的其他位置。
<?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,
);
}
}
?>
The answer was given to me by awesome #drupal chanel (thanks @graper =) ) 答案是由很棒的#drupal chanel给我的(感谢@graper =))
What's wrong is that: 出问题的是:
$form['field_assigned_license'] = array(
'#title' => t('Assigned Licence'),
'#type' => 'textfield',
'#default_value' => ($_REQUEST['lid']) ? $_REQUEST['lid']: '',
'#required' => ($_REQUEST['lid']) ? 1:0,
);
will basically override everything that is saved in $form['field_assigned_license']
. 基本上会覆盖保存在
$form['field_assigned_license']
。 The correct approach is to just override the certain parameter I want, eg $form['field_assigned_customer']['und'][0]['value']['#default_value']
or merge the original array with the adjustments. 正确的方法是仅覆盖我想要的某些参数,例如
$form['field_assigned_customer']['und'][0]['value']['#default_value']
或将原始数组与调整合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.