1

I am a drupal newbie and am having the following problem:

In my custom content type page, I have multiple check boxes and a text area, among other fields. What I want is that, I would like the values of the checked check-boxes to be pasted inside the text area. The checkboxes are created as a multi-value field (field type = list, widget = check-box). Basically what I want is, every time the user checks/unchecks one of the checkboxes, the text area should re-populate with values of all the checkboxes that are currently selected.

I tried using the dependency fields module, but it did not solve my problem. Have anyone encountered this need before?

Based on what I read, it seems this might be possible using hook_node_presave, however I am not sure where to add this. I am using a template called norma, so I edited its template.php and added a function like so:

function norma_node_presave($node)
{ 
 print_r($node);
 exit;
}

However, nothing has happened... :(

Any pointers on how to solve this is very much appreciated...

2
  • Are the text fields hidden? Or do you want it to be "check a checkbox" -> add the value of checkbox -> set field value.
    – Steven
    Commented Sep 24, 2013 at 12:53
  • it is a regular text area. Suppose there are three checkboxes with values 1,2 and 3 and suppose 1 and 3 are selected. Then in my text area I want the value to be "1 3" Commented Sep 24, 2013 at 13:01

2 Answers 2

3

hook_node_presave($node) should be included in a custom module and not in your theme !

Look at Creating Drupal 7.x modules if you want to try so.

In your hook do something like that :

function norma_node_presave($node){ 

  $value = array();
  foreach( $node->field_checkbox['und'] as $checkbox){
    $value[] = $checkbox['value'];
  }

  $node->field_textfield['und'][0]['value'] = implode('<br>', $value);
}
1
  • That is just what I was looking for, thanks a million!! :) Commented Sep 24, 2013 at 13:35
0

The best idea would be to use Javascript (so that as you check the checkbox the field value changes), you can do something like this.

$(document).ready(function() {
    $('#checkbox1').change(function() {
        $('.field1').val($(this).val());  
    });
});

Or something like that, I can't guarantee that code works but it should get you started.

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.