You are here

field.inc in Feeds 7

Same filename and directory in other branches
  1. 7.2 mappers/field.inc

On behalf implementation of Feeds mapping API for field.module.

Does actually not include mappers for field types defined in fields module (because there aren't any) but mappers for all fields that contain their value simply in $entity->fieldname['und'][$i]['value'].

File

mappers/field.inc
View source
<?php

/**
 * @file
 * On behalf implementation of Feeds mapping API for field.module.
 *
 * Does actually not include mappers for field types defined in fields module
 * (because there aren't any) but mappers for all fields that contain their
 * value simply in $entity->fieldname['und'][$i]['value'].
 */

/**
 * Implements hook_feeds_processor_targets_alter().
 *
 * @see FeedsNodeProcessor::getMappingTargets().
 */
function field_feeds_processor_targets_alter(&$targets, $entity_type, $content_type) {
  foreach (field_info_instances($entity_type, $content_type) as $name => $instance) {
    $info = field_info_field($name);
    $allowed_types = array(
      'number_integer',
      'number_decimal',
      'number_float',
      'text',
      'text_long',
      'text_with_summary',
    );
    if (in_array($info['type'], $allowed_types)) {
      $targets[$name] = array(
        'name' => $instance['label'],
        'callback' => 'field_feeds_set_target',
        'description' => t('The @label field of the node.', array(
          '@label' => $instance['label'],
        )),
      );
    }
  }
}

/**
 * Callback for mapping. Here is where the actual mapping happens.
 *
 * When the callback is invoked, $target contains the name of the field the
 * user has decided to map to and $value contains the value of the feed item
 * element the user has picked as a source.
 */
function field_feeds_set_target($entity, $target, $value) {
  if (empty($value)) {
    return;
  }

  // Handle non-multiple value fields.
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }
  $info = field_info_field($target);

  // Iterate over all values.
  $i = 0;
  $field = isset($entity->{$target}) ? $entity->{$target} : array();
  foreach ($value as $v) {
    if (!is_array($v) && !is_object($v)) {
      $field['und'][$i]['value'] = $v;
    }
    if ($info['cardinality'] == 1) {
      break;
    }
    $i++;
  }
  $entity->{$target} = $field;
}

Functions

Namesort descending Description
field_feeds_processor_targets_alter Implements hook_feeds_processor_targets_alter().
field_feeds_set_target Callback for mapping. Here is where the actual mapping happens.