Projet

Général

Profil

Paste
Télécharger (2,79 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / mappers / file.inc @ 2c8c2b87

1
<?php
2

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

    
13
/**
14
 * Implements hook_feeds_processor_targets_alter().
15
 *
16
 * @see FeedsNodeProcessor::getMappingTargets().
17
 */
18
function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
19
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
20
    $info = field_info_field($name);
21

    
22
    if (in_array($info['type'], array('file', 'image'))) {
23
      $targets[$name] = array(
24
        'name' => check_plain($instance['label']),
25
        'callback' => 'file_feeds_set_target',
26
        'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
27
      );
28
    }
29
  }
30
}
31

    
32
/**
33
 * Callback for mapping. Here is where the actual mapping happens.
34
 *
35
 * When the callback is invoked, $target contains the name of the field the
36
 * user has decided to map to and $value contains the value of the feed item
37
 * element the user has picked as a source.
38
 */
39
function file_feeds_set_target($source, $entity, $target, $value) {
40
  if (empty($value)) {
41
    return;
42
  }
43
  module_load_include('inc', 'file');
44

    
45
  // Make sure $value is an array of objects of type FeedsEnclosure.
46
  if (!is_array($value)) {
47
    $value = array($value);
48
  }
49
  foreach ($value as $k => $v) {
50
    if (!($v instanceof FeedsEnclosure)) {
51
      if (is_string($v)) {
52
        $value[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
53
      }
54
      else {
55
        unset($value[$k]);
56
      }
57
    }
58
  }
59
  if (empty($value)) {
60
    return;
61
  }
62

    
63
  $entity_type = $source->importer->processor->entityType();
64

    
65
  // Determine file destination.
66
  // @todo This needs review and debugging.
67
  $instance_info = field_info_instance($entity_type, $target, $source->importer->processor->bundle());
68
  $info = field_info_field($target);
69
  $data = array();
70
  if (!empty($entity->uid)) {
71
    $data[$entity_type] = $entity;
72
  }
73
  $destination = file_field_widget_uri($info, $instance_info, $data);
74

    
75
  // Populate entity.
76
  $i = 0;
77
  $field = isset($entity->$target) ? $entity->$target : array();
78
  foreach ($value as $v) {
79
    $file = FALSE;
80
    try {
81
      $v->setAllowedExtensions($instance_info['settings']['file_extensions']);
82
      $file = $v->getFile($destination);
83
    }
84
    catch (Exception $e) {
85
      watchdog('feeds', check_plain($e->getMessage()));
86
    }
87
    if ($file) {
88
      $field['und'][$i] = (array)$file;
89
      $field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field.
90
      if ($info['cardinality'] == 1) {
91
        break;
92
      }
93
      $i++;
94
    }
95
  }
96
  $entity->{$target} = $field;
97
}