I have a cck list(text) field. Widget: select I already have data in the database and I want to add a few more options to the select list. I've tried using hook_form_alter but I can't seem to change the options. Here is my code:
function color_cats_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "cats_node_form") {
$form['#after_build'][] = 'color_cats_after_build';
}
}
function color_cats_after_build($form, &$form_state) {
dsm($form);
$form['field_color_cat']['und']['#options']['brown'] ='Brown';
$form['field_color_cat']['und']['#options']['white'] ='White';
$form['field_color_cat']['und']['#options']['gold'] ='Gold';
$form['field_color_cat']['und']['#options']['grey'] ='Grey';
$form['field_color_cat']['und']['#options']['tabby'] ='Tabby';
$form['field_color_cat']['und']['#options']['ginger'] ='Ginger';
$form['field_color_cat']['und']['#options']['multi-colored'] ='Multi Colored';
return $form;
}
dsm($form)
is showing so the function color_cats_after_build
is being called. The issue is $form['field_color_cat']['und']['#options']['brown'] ='Brown';
is not affecting the select list.
dsm($form)
shows
field_color_cat (Array, 17 elements)
#type (String, 9 characters ) container
#attributes (Array, 1 element)
#weight (String, 2 characters ) 16
#tree (Boolean) TRUE
#language (String, 3 characters ) und
und (Array, 38 elements)
#entity (Object) stdClass
#entity_type (String, 4 characters ) node
#bundle (String, 4 characters ) cats
#field_name (String, 15 characters ) field_color_cat
#language (String, 3 characters ) und
#field_parents (Array, 0 elements)
#columns (Array, 1 element)
#title (String, 5 characters ) Color
#description (String, 56 characters ) This is the old color selection and SHOULD NOT ...
#required (Boolean) FALSE
#delta (Integer) 0
#type (String, 6 characters ) select
#default_value (Array, 0 elements)
#multiple (Boolean) FALSE
#options (Array, 6 elements)
_none (String, 8 characters ) - None -
Black (String, 5 characters ) Black
Brown (String, 5 characters ) Brown
Gold (String, 4 characters ) Gold
White (String, 5 characters ) White
Multi Colored (String, 13 characters ) Multi Colored
#value_key (String, 5 characters ) value
#element_validate (Array, 1 element)
#properties (Array, 4 elements)
#after_build (Array, 1 element)
#input (Boolean) TRUE
#process (Array, 2 elements)
#theme (String, 6 characters ) select
#theme_wrappers (Array, 1 element)
#pre_render (Array, 1 element)
#defaults_loaded (Boolean) TRUE
#tree (Boolean) TRUE
#parents (Array, 2 elements)
#array_parents (Array, 2 elements)
#weight (Integer) 0
#processed (Boolean) TRUE
#attributes (Array, 0 elements)
#title_display (String, 6 characters ) before
#id (String, 24 characters ) edit-field-color-cat-und
#name (String, 20 characters ) field_color_cat[und]
#value (Array, 0 elements)
#ajax_processed (Boolean) FALSE
#sorted (Boolean) TRUE
#after_build_done (Boolean) TRUE
#access (Boolean) TRUE
#theme_wrappers (Array, 1 element)
#process (Array, 1 element)
#defaults_loaded (Boolean) TRUE
#parents (Array, 1 element)
#array_parents (Array, 1 element)
#processed (Boolean) TRUE
#required (Boolean) FALSE
#title_display (String, 6 characters ) before
#id (String, 20 characters ) edit-field-color-cat
#sorted (Boolean) TRUE
How do I add values to my select list?
Thanks
This is my code just using the hook_form_alter:
function color_cats_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == "cats_node_form") {
$form['field_color_cat']['und']['#options'] = array(
'All' => t('- Any -'),
'Black' => t('Black'),
'Brown' => t('Brown'),
'White' => t('White'),
'Gold' => t('Gold'),
'Grey' => t('Grey'),
'Tabby' => t('Tabby'),
'Ginger' => t('Ginger'),
'Multi Colored' => t('Multi Colored')
); }}
The select list is showing all the values and it works if I select an old value, but if I select a new value that I added eg ginger then I can the error message: color:illegal value.