1

I need the checkbox to be checked by default. Below is my code, notice the 'default_value' under the settings array.

$field_instance = array(                                      
    'field_name'    => $fielddef['field_name'],               
    'type'          => 'list_boolean',                        
    'entity_type'   => 'user',                                
    'bundle'        => 'user',                                
    'label'         => t($opts['label']),                     
    'description'   => t($opts['description']),               
    'required'      => FALSE,                                 
    'widget'        => array(                                 
        'type'      => 'options_onoff',                       
        'weight'    => 10,                                    
    ),                                                        
    'settings'      => array(                                 
        'allowed_values' => drupal_map_assoc(range(0,1)),     
        'default_value' => array(array('value' => 1))         
    )                                                         
);
field_create_instance($field_instance); 

I have tried using 'default_value' in the $field_instance array, I've also tried 'default_value_function' without success. I feel like I'm close on this, what am I missing? Thanks!

Also, I am running Drupal 7.23 as shown on my Status Report. I asked this question earlier on SO, but no reply so I moved it here: https://stackoverflow.com/q/19772646/988355

2
  • Huh! Have you tried just setting the default value property to TRUE or 1? What I usually do when I run into a problem with programatically instantiating field instances is to use something similar to the code in this blog post on a UI-configured field: steindom.com/articles/… The debug output from that should show you how the default value property wants to be set. Features will also give you some handy output that is similar to the aformentioned code snippet. Nov 5, 2013 at 0:05
  • You mean to say the 'default_value' property? Can you be more specific? Or maybe the '#default_value' or just 'default'? I will try this tomorrow and let you know. Thanks! Nov 5, 2013 at 3:06

1 Answer 1

0

Try with this

$field_instance = array(                                      
'field_name'    => $fielddef['field_name'],               
'type'          => 'list_boolean',                        
'entity_type'   => 'user',                                
'bundle'        => 'user',                                
'label'         => t($opts['label']),                     
'description'   => t($opts['description']),               
'required'      => FALSE,                                 
'widget'        => array(                                 
    'type'      => 'options_onoff',                       
    'weight'    => 10,                                    
),                                                        
'settings'      => array(                                 
    'allowed_values' => drupal_map_assoc(range(0,1)),     
    'default_value' => array(1),         
)                                                         

);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.