-1

When I add new boolean field to content type in admin i can choose widget for it: checkbox or radio buttons. How can I describe it in code? When I do something like this there is no widget:

$i = 1; 
foreach($questions as $question){
        $field_name = $name . '_q' . $i;
        $fields[$field_name] = array(
            'field_name' => $field_name,
            'type' => 'list_boolean',
            'required' => true,
        );
        $i++;
}

foreach($fields as $field){
        if (!field_info_field($field['field_name'])) {
            field_info_cache_clear();
            field_create_field($field);
        }
}

$instances = array();

$i = 1;
foreach($questions as $question){
    $field_name = $name . '_q' . $i;
    $instances[$field_name] = array(
        'field_name' => $field_name,
        'label' => $question,
        'widget' => array(
            'type' => 'radio'
         ),
         'required' => true
     );
     $i++;
}

foreach($instances as $instance){
    $instance['entity_type'] = 'node';
    $instance['bundle'] = $name;
    field_create_instance($instance);
}
  • What does "describe it in code" mean? – ya.teck Jul 30 '13 at 7:18
  • I create content type and add fields for it in module. – Dmitrii Mikhailov Jul 30 '13 at 7:35
  • What do you mean "no widget"? Field is there, appears in tpl.php and Views, but can't be edited by user? – Mołot Jul 30 '13 at 7:39
  • There is a label for field but nothing else. – Dmitrii Mikhailov Jul 30 '13 at 7:44
  • I found that for checkboxes i need to use widget type "options_onoff". But how to make it radios? – Dmitrii Mikhailov Jul 30 '13 at 7:46
0

Simplest way is to use field_create_field() to create the field itself and field_create_instance($instance) to have an instance for your content type. Widget type should be set in $instance['widget']['type'] sent to field_create_instance($instance).

Taken from this answer.

  • I do these things. It's not what I'm asking about. I can't figure out how to make my boolean fields look like radio buttons in form. – Dmitrii Mikhailov Jul 30 '13 at 7:11
  • @DmitryMikhaylov if you are passing radio widget type to the field_create_instance(), please show relevant code. And if you want conditionally programmatically change widget (or not), please tell where / when you want to execute it and should this change be permanent or temporary. – Mołot Jul 30 '13 at 7:26
  • I've added this code now. – Dmitrii Mikhailov Jul 30 '13 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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