You are here

public function FeedsTermProcessor::setTargetElement in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsTermProcessor.inc \FeedsTermProcessor::setTargetElement()

Overrides parent::setTargetElement().

Operate on a target item that is a taxonomy term.

Overrides FeedsProcessor::setTargetElement

File

plugins/FeedsTermProcessor.inc, line 109
FeedsTermProcessor class.

Class

FeedsTermProcessor
Feeds processor plugin. Create taxonomy terms from feed items.

Code

public function setTargetElement(FeedsSource $source, $target_term, $target_element, $value, array $mapping = array()) {
  switch ($target_element) {
    case 'parent':
      if (!empty($value)) {
        $terms = taxonomy_get_term_by_name($value);
        $parent_tid = '';
        foreach ($terms as $term) {
          if ($term->vid == $target_term->vid) {
            $parent_tid = $term->tid;
          }
        }
        if (!empty($parent_tid)) {
          $target_term->parent[] = $parent_tid;
        }
        else {
          $target_term->parent[] = 0;
        }
      }
      else {
        $target_term->parent[] = 0;
      }
      break;
    case 'parentguid':

      // Value is parent_guid field value.
      $parent_tid = 0;
      $query = db_select('feeds_item')
        ->fields('feeds_item', array(
        'entity_id',
      ))
        ->condition('entity_type', $this
        ->entityType());
      $term_ids = array_keys($query
        ->condition('guid', $value)
        ->execute()
        ->fetchAllAssoc('entity_id'));
      if (!empty($term_ids)) {
        $terms = entity_load($this
          ->entityType(), $term_ids);
        foreach ($terms as $term) {
          if ($term->vid == $target_term->vid) {
            $parent_tid = $term->tid;
            break;
          }
        }
      }
      $target_term->parent[] = $parent_tid;
      break;
    case 'weight':
      if (!empty($value)) {
        $weight = intval($value);
      }
      else {
        $weight = 0;
      }
      $target_term->weight = $weight;
      break;
    case 'description':
      if (!empty($mapping['format'])) {
        $target_term->format = $mapping['format'];
      }
      elseif (!empty($this->config['input_format'])) {
        $target_term->format = $this->config['input_format'];
      }
      else {
        $target_term->format = filter_fallback_format();
      }
      $target_term->description = $value;
      break;
    default:
      parent::setTargetElement($source, $target_term, $target_element, $value);
      break;
  }
}