You are here

function file_feeds_set_target in Feeds 7.2

Same name and namespace in other branches
  1. 8.2 mappers/file.inc \file_feeds_set_target()
  2. 7 mappers/file.inc \file_feeds_set_target()

Callback for mapping file fields.

1 string reference to 'file_feeds_set_target'
file_feeds_processor_targets in mappers/file.inc
Implements hook_feeds_processor_targets().

File

mappers/file.inc, line 84
On behalf implementation of Feeds mapping API for file.module and image.module.

Code

function file_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  $language = $mapping['language'];
  $mapping += array(
    'file_exists' => FILE_EXISTS_RENAME,
  );

  // Add default of uri for backwards compatibility.
  list($field_name, $sub_field) = explode(':', $target . ':uri');
  $info = field_info_field($field_name);
  if ($sub_field == 'uri') {
    foreach ($values as $k => $v) {
      if (!$v instanceof FeedsEnclosure) {
        if (!empty($v) && is_string($v)) {
          $values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
        }
        else {

          // Set the value for FALSE rather than remove it to keep our deltas
          // correct.
          $values[$k] = FALSE;
        }
      }
    }
    if ($entity instanceof Entity) {
      $entity_type = $entity
        ->entityType();
      $bundle = $entity
        ->bundle();
    }
    else {
      $entity_type = $source->importer->processor
        ->entityType();
      $bundle = $source->importer->processor
        ->bundle();
    }
    $instance_info = field_info_instance($entity_type, $field_name, $bundle);

    // Determine file destination.
    // @todo This needs review and debugging.
    $data = array();
    if (!empty($entity->uid)) {
      $data[$entity_type] = $entity;
    }
    $destination = file_field_widget_uri($info, $instance_info, $data);
  }

  // Populate entity.
  $field = isset($entity->{$field_name}) ? $entity->{$field_name} : array(
    $language => array(),
  );
  $delta = 0;
  foreach ($values as $v) {
    if ($info['cardinality'] == $delta) {
      break;
    }
    if (!isset($field[$language][$delta])) {
      $field[$language][$delta] = array();
    }
    switch ($sub_field) {
      case 'alt':
      case 'title':
      case 'description':
        $field[$language][$delta][$sub_field] = $v;
        break;
      case 'uri':
        $skip = FALSE;
        if ($v) {
          if ($mapping['file_exists'] == FEEDS_FILE_EXISTS_SKIP) {
            if (file_exists($destination . '/' . basename($v
              ->getValue()))) {
              $skip = TRUE;
            }
            else {

              // We already know the file doesn't exist so we don't have to
              // worry about anything being renamed, but we do need a valid
              // replace value for file_save().
              $mapping['file_exists'] = FILE_EXISTS_RENAME;
            }
          }
          if ($mapping['file_exists'] == FEEDS_FILE_EXISTS_REPLACE_DIFFERENT) {
            if (file_exists($destination . '/' . basename($v
              ->getValue())) && file_feeds_file_compare($v
              ->getValue(), $destination . '/' . basename($v
              ->getValue()))) {
              $skip = TRUE;
            }
            else {

              // Either the file doesn't exist or it does and it's different.
              $mapping['file_exists'] = FILE_EXISTS_REPLACE;
            }
          }
          if ($mapping['file_exists'] == FEEDS_FILE_EXISTS_RENAME_DIFFERENT) {
            if (file_exists($destination . '/' . basename($v
              ->getValue())) && file_feeds_file_compare($v
              ->getValue(), $destination . '/' . basename($v
              ->getValue()))) {
              $skip = TRUE;
            }
            else {

              // Either the file doesn't exist or it does and it's different.
              $mapping['file_exists'] = FILE_EXISTS_RENAME;
            }
          }
          if ($skip) {

            // Create a new dummy feeds enclosure where the value is the file
            // already in the file system (it will be skipped by getFile()).
            $mapping['file_exists'] = FEEDS_FILE_EXISTS_SKIP;
            $existing_path = $destination . '/' . basename($v
              ->getValue());
            $v = new FeedsEnclosure($existing_path, file_get_mimetype($existing_path));
          }
          try {
            $v
              ->setAllowedExtensions($instance_info['settings']['file_extensions']);
            $field[$language][$delta] += (array) $v
              ->getFile($destination, $mapping['file_exists']);

            // @todo: Figure out how to properly populate this field.
            $field[$language][$delta]['display'] = 1;
          } catch (Exception $e) {
            watchdog('feeds', check_plain($e
              ->getMessage()));
          }
        }
        break;
    }
    $delta++;
  }
  $entity->{$field_name} = $field;
}