Hi i had created 1 content type. In this content type I have created a field called courses of type field collections. In the field collections I have added 1 fields of type checkbox which I want to be enabled by default. there is option to do by configuration. But I want to do it by coding . How to do?
2 Answers
You can this using Drupal's Form API. You'll have to alter the form and set the value.
function modulename_form_alter(&$form, &$form_state, $form_id){
switch($form_id){
case "your_form":
$form['cbs'] = array(
'#type' => 'checkboxes',
'#options' => array(1 => "One", 2 => "Two", 3 => "Three"),
'#default_value' => array(1, 3),
);
break;
}
}
To know more about altering form check the below links:
- http://befused.com/drupal/form-alter
- http://themery.com/dgd7/advanced-theming/forms/with-alter-hooks
UPDATE:
If field collections try using widget_form_alter:
function my_module_field_widget_form_alter(&$element, &$form_state, $context) {
if ("field_my_field_collection" == $element['#bundle']) {
if ("field_my_field_collection" == $element['#field_name']) {
//do your work here
}
}
}
There are two related questions in the following links, which may help you:
-
Hii, it is working for fields of that content type. But as I mentioned I Created 1 course field of type field collection.in the field collection I have added 1 checkbox. That checkbox I want to be enabled by default. Commented Jun 14, 2017 at 10:16
-
if($form_id == 'course_page_node_form') {
$form['field_course_tabs'][LANGUAGE_NONE][0]['field_enable_in_tab'][LANGUAGE_NONE]['#default_value'] = 1;
} this one worked for me.