I've the next form.

  $query = "SELECT name, tid FROM taxonomy_term_data WHERE vid=2 AND language = '$language_url';";
  $result = db_query($query);
  foreach ($result as $row2) {
     $options2[$row2->tid] = t($row2->name);
  $form['group_config']['municipios'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Municipios'),
     '#options' => $options2,
     '#required' => TRUE,

I do a query to take the differents taxonomy terms that I have.

I don't know how to obtain the values that the user has selected and save into a field.

I've done the next code but It dosn't work.

   function anuncio_datos_usuario(&$form_state,$values){
        $node = new stdClass();
        $node->title = $values['titulo'];
    $node->type = $type;
    $node->language = $values['idioma'];
    $node->uid = $userid;
    $node->status = 0; //(1 or 0): published or not
    $node->promote = 0; //(1 or 0): promoted to front page
    $node->sticky = 0;
        foreach($values['municipios'] as $municipio){
                $node->field_municipio[LANGUAGE_NONE][]['tid']= $municipio;

    $node = node_submit($node); // Prepare node for saving

How can I save all the selected values into the field?

Thank you.

  • Could you post the whole code? First the hook_form implementation and the hook_form_submit function you use to save the values? – Pontus Nilsson May 19 '14 at 11:13
  • The question has been edited. The first code is a part from hook_form (the important one, I have an other forms. The second one is the function to save the values. I've saved all the values correctly, but I don't know how to save checkbox type values. – more770 May 19 '14 at 11:19

I think you what you want is an entity reference field.

Go the manage fields for your content type and create a new field of the type entity reference. In the setting for number of values choose unlimited and as widget choose checkboxes.

This will save the values for you, and display the chosen ones when the node is edited.

  • This is alredy created. In my module what I do is create a node of a expecific type. To create all the fields, I have done a content type to create automatically all the fields. One of the fields is municipio, that is checkbox type and has unlimited values. What I don't know is how to save into that field more that one value, because in the checkbox I can select more than one... – more770 May 19 '14 at 11:31

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.