diff --git a/pmpapi_pull/classes/PMPAPIDrupalPull.php b/pmpapi_pull/classes/PMPAPIDrupalPull.php index 9f50a15..86bf751 100644 --- a/pmpapi_pull/classes/PMPAPIDrupalPull.php +++ b/pmpapi_pull/classes/PMPAPIDrupalPull.php @@ -316,33 +316,35 @@ class PMPAPIDrupalPull extends PMPAPIDrupal { $default_format = filter_default_format(user_load($this->uid)); $local_field_info = field_info_field($local_field); - if ($pmp_type == 'array') { - if ($local_field_info['type'] == 'taxonomy_term_reference') { - $tids = array(); - $vocab_name = $local_field_info['settings']['allowed_values'][0]['vocabulary']; - $vocab = taxonomy_vocabulary_machine_name_load($vocab_name); - $vid = $vocab->vid; - + // Right now this only handles taxonomy reference, but should handle all + // entity references (for at least other nodes and users). + $reference_types = array( + 'taxonomy_term_reference' => 'tid', + ); + + // If this is being pushed to a reference type, look up the target by title + // and map. + if (array_key_exists($local_field_info['type'], $reference_types)) { + $tids = array(); + $vocab_name = $local_field_info['settings']['allowed_values'][0]['vocabulary']; + + if (is_array($doc->attributes->$pmp_field)) { foreach ($doc->attributes->$pmp_field as $element) { - $terms = taxonomy_get_term_by_name($element, $vocab_name); - if (!empty($terms)) { - $term = array_shift($terms); - $tids[] = array('tid' => $term->tid); - } - else { - $term = new stdClass(); - $term->name = $element; - $term->vid = $vid; - $term->vocabulary_machine_name = $vocab->machine_name; - taxonomy_term_save($term); - $tids[] = array('tid' => $term->tid); - } + $tid = $this->taxonomyLookup($element, $vocab_name); + $tids[] = array('tid' => $tid); } - $value = array($entity->language => $tids); } else { - $value = implode(', ', $doc->$pmp_field); + $tid = $this->taxonomyLookup($doc->attributes->$pmp_field, $vocab_name); + $tids[] = array('tid' => $tid); } + $value = array($entity->language => $tids); + + } + elseif ($pmp_type == 'array') { + // If the source is an array and we aren't making it reference, convert + // to string. + $value = implode(', ', $doc->$pmp_field); } else { $values = array( @@ -352,9 +354,28 @@ class PMPAPIDrupalPull extends PMPAPIDrupal { $this->alterMappedValues($values, $local_field_info, $entity, $doc->profile); $value = array($entity->language => array(0 => $values)); } + $entity->$local_field = $value; } + private function taxonomyLookup($term_name, $vocab_name) { + $terms = taxonomy_get_term_by_name($term_name, $vocab_name); + if (!empty($terms)) { + $term = array_shift($terms); + return $term->tid; + } + else { + $vocab = taxonomy_vocabulary_machine_name_load($vocab_name); + $term = new stdClass(); + $term->name = $term_name; + $term->vid = $vocab->vid; + $term->vocabulary_machine_name = $vocab->machine_name; + taxonomy_term_save($term); + return $term->tid; + } + + } + /** * Alter values just prior to mapping. *