diff --git a/services/ImportService.php b/services/ImportService.php index a8a97f4..ea2e7df 100644 --- a/services/ImportService.php +++ b/services/ImportService.php @@ -442,6 +442,50 @@ public function prepForFieldType(&$data, $handle) } break; + + case ImportModel::FieldTypeTags: + + // Get field settings + $settings = $field->getFieldType()->getSettings(); + + // Get tag group id + $source = $settings->getAttribute('source'); + list($type, $groupId) = explode(':', $source); + + $tags = ArrayHelper::stringToArray($data); + $data = array(); + + foreach($tags as $tag){ + + // Find existing tag + $criteria = craft()->elements->getCriteria(ElementType::Tag); + $criteria->title = $tag; + $criteria->groupId = $groupId; + + if(!$criteria->total()) { + + // Create tag if one doesn't already exist + $newtag = new TagModel(); + $newtag->getContent()->title = $tag; + $newtag->groupId = $groupId; + + // Save tag + if(craft()->tags->saveTag($newtag)) { + $tagArray = array($newtag->id); + } + + } else { + + $tagArray = $criteria->ids(); + + } + + // Add tags to data array + $data = array_merge($data, $tagArray); + + } + + break; case ImportModel::FieldTypeNumber: @@ -495,50 +539,6 @@ public function prepForFieldType(&$data, $handle) break; - case ImportModel::FieldTypeTags: - - //get settings - $settings = $field->getFieldType()->getSettings(); - - //get tag group id - $source = $settings->getAttribute('source'); - list($type, $groupId) = explode(':', $source); - - $tags = ArrayHelper::stringToArray($data); - $data = array(); - - foreach($tags as $tag){ - - // Find existing tag - $criteria = craft()->elements->getCriteria(ElementType::Tag); - $criteria->title = $tag; - $criteria->groupId = $groupId; - - if(!$criteria->total()) { - - // Create tag if one doesn't already exist - $newtag = new TagModel(); - $newtag->getContent()->title = $tag; - $newtag->groupId = $groupId; - - // Save tag - if(craft()->tags->saveTag($newtag)) { - $tagArray = array($newtag->id); - } - - } else { - - $tagArray = $criteria->ids(); - - } - - // Add tags to data array - $data = array_merge($data, $tagArray); - - } - - break; - } }