-1

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?

0

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:

  1. http://befused.com/drupal/form-alter
  2. 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:

  1. Question One.
  2. Question Two.
2
  • 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. Jun 14 '17 at 10:16
  • @shailjaranjan I have added further details.
    – esafwan
    Jun 14 '17 at 15:29
0
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.

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.