I have the following field (as an example) created with Field API, which works great. 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
.
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.
<?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 =) )
What's wrong is that:
will basically override everything that is saved in
$form['field_assigned_license']
. The correct approach is to just override the certain parameter I want, e.g.$form['field_assigned_customer']['und'][0]['value']['#default_value']
or merge the original array with the adjustments.