0

I have a field that is a taxonomy term reference with a checkbox widget that allows multiple values.

I am looping through a json structure, finding the associated code value and then setting the value on the checkbox widget in the form with a case statement.

My problem is it's only setting the last value, not all the values. Here is the code. In this case where the hosts array contains all three values, I would expect all checkboxes associated with all three taxonomy terms to be checked. Only the last one is checked.

//json structure
"hosts": [
    {
        "alias": "external",
        "url": "abc.com"
    },
    {
        "alias": "internal",
        "url": "abc.com"
    }
    {
        "alias": "local",
        "url": "abc.com"
    }
] 

          //php
          $hosts = $response->{'hosts'};

           foreach($hosts as $host){

           $alias = $host->alias;

            switch($alias){
                case "external":
                    form_set_value($form['field_proxy_host'], array(LANGUAGE_NONE => array(0 => array('tid' => '758'))), $form_state);
                break;
                case "internal":
                    form_set_value($form['field_proxy_host'], array(LANGUAGE_NONE => array(0 => array('tid' => '759'))), $form_state);
                break;
                case "local":
                    form_set_value($form['field_proxy_host'], array(LANGUAGE_NONE => array(0 => array('tid' => '760'))), $form_state);
                break;

            }


        }  
  • Where does the $host variable come from? – Jdrupal Apr 18 '18 at 16:04
  • I updated the code to better reflect hosts. It's actually an api call that returns a json object . – dfish17 Apr 18 '18 at 16:07
  • form_set_value sets the value, it does not append the value to the values already there. Each time you call form_set_value it clears the previous values and sets the value to your new value. – sonfd Apr 18 '18 at 16:10
  • What you are doing doesnt really make sense because of cause will the variable be overwritten. The same variable cannot have three different values. – Jdrupal Apr 18 '18 at 16:14
2

I believe you want to pass an array with all values to form_set_value() as the function is setting the value, not appending to it. I would alter your setup to build an array of values with your switch/foreach block and then pass that array in a single form_set_value call below the switch, outside of the foreach.

Update your your foreach / switch like so:

$host_values = [];
foreach($hosts as $host){
  switch($host->alias){
    case "external":
      $host_values[] = ['tid' => '758'];
      break;
    case "internal":
      $host_values[] = ['tid' => '759']
      break;
    case "local":
      $host_values[] = ['tid' => '760']
      break;
    default:
      break;
  }
}

form_set_value($form['field_proxy_host'], [LANGUAGE_NONE => $host_values], $form_state);
  • OK, any idea on how to do that in form_set_value? For my value parameter? If I add to another array say $hosts_values ('758', '759', '760') //numbers being tid's, how do I pass those as a group to be set in value? – dfish17 Apr 18 '18 at 16:17
  • Updated my answer with the code example for your use case. Basically you build an array of the $host_values and then you pass that array into $form_set_value() as seen above. In your original post you are building this array manually each time you call form_set_value, but the array always only had a single value, e.g. array(0 => array('tid' => '758'))). – sonfd Apr 18 '18 at 16:37
  • Sure, np. You should accept the answer so it shows as resolved. :) – sonfd Apr 18 '18 at 16:47

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.