1

I have a field_collection field attached to user profiles. This is an "unlimited" field, so, the user can add as many addresses as they like via their profile page. However, I also need to add addresses to their profile programmatically from elsewhere (in a module). I have the following code:

    function _cnm_address_add_user_address($address=array()) {
      //Load the controller class file.
      module_load_include('inc', 'entity', 'includes/entity.controller');

        $values = array();
      $user = user_uid_optional_load();
      if(is_object($user)){
        //Get any previously saved field values to make sure they are not overwritten
        //while we attempt to save the new one
        $count = 0;    
        if(!empty($user->field_cnm_address['und'][0]['value'])){
          foreach($user->field_cnm_address['und'] as $fc){
            $fc_obj = field_collection_item_load($fc['value']);
            //If any exiting field values are empty, default to empty string ''
            $fname = (!empty($fc_obj->field_cnm_address_fname['und'][0]['value']))
                ? $fc_obj->field_cnm_address_fname['und'][0]['value'] : '';
            $lname = (!empty($fc_obj->field_cnm_address_lname['und'][0]['value']))
                ? $fc_obj->field_cnm_address_lname['und'][0]['value'] : '';
            $country_tid = (!empty($fc_obj->field_cnm_address_country['und'][0]['tid']))
                ? $fc_obj->field_cnm_address_country['und'][0]['tid'] : 0;
            $state_tid = (!empty($fc_obj->field_cnm_address_state['und'][0]['tid']))
                ? $fc_obj->field_cnm_address_state['und'][0]['tid'] : 0;
            //Add fields to $values array
                  $values[$count] = array(
                    'field_cnm_address_fname' => array('und' => array(array('value' => $fname)),),
                    'field_cnm_address_lname' => array('und' => array(array('value' => $lname)),),
                    'field_cnm_address_country' => array('und' => array(array('tid' => $country_tid)),),
                    'field_cnm_address_state' => array('und' => array(array('tid' => $state_tid)),),
                  );
                  //Set the field_collection field_name for this set of fields
                  $values[$count]['field_name'] = 'field_cnm_address';
                  $count++;
          }
        }

            //The new entry...
          $values[$count] = array(
            'field_cnm_address_fname' => array('und' => array(array('value' => $address['fname'])),),
            'field_cnm_address_lname' => array('und' => array(array('value' => $address['lname'])),),
            'field_cnm_address_country' => array('und' => array(array('tid' => $address['country'])),),
            'field_cnm_address_state' => array('und' => array(array('tid' => $address['state'])),),
          );
          //Set the field_collection field_name for this new entry
            $values[$count]['field_name'] = 'field_cnm_address';

        //Save all values
            foreach ($values as $key => $value) {
                $field_entity = entity_create('field_collection_item', $value);
                $field_entity->setHostEntity('user', $user);
                $field_entity->save();
            }       
        }
    }

Here is my problem: There doesn't seem to be any issue with adding regular string values (such as the first name field, which uses ['und'][0]['value']), however, my country and state fields are term reference fields and when I try to add the tid by using ['und'][0]['tid'] (which is the format that the field collection uses itself -- see how I retrieved previous values from the fields), I get the following error:

Notice: Undefined property: stdClass::$name in taxonomy_term_save() (line 628 of /.../modules/taxonomy/taxonomy.module).
Notice: Undefined property: stdClass::$vid in taxonomy_term_save() (line 630 of /.../modules/taxonomy/taxonomy.module).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of /.../includes/entity.inc).
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of /.../includes/entity.inc).
Notice: Trying to get property of non-object in taxonomy_term_save() (line 631 of /.../modules/taxonomy/taxonomy.module).
EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 7818 of /.../includes/common.inc).

It appears to be trying to save a new taxonomy term!? However, that is NOT what I want. Instead, I am trying to assign an existing taxonomy term tid to the field within a field_collection, not save a new term object.

Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.