Projet

Général

Profil

Révision 41cc1b08

Ajouté par Assos Assos il y a presque 9 ans

Update feeds 7.x-2.0-alpha9 -> 7.x-2.0-beta1

Install lib simplepie 1.3.1

Voir les différences:

drupal7/sites/all/modules/feeds/mappers/file.inc
4 4
 * @file
5 5
 * On behalf implementation of Feeds mapping API for file.module and
6 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 7
 */
12 8

  
13 9
/**
14
 * Implements hook_feeds_processor_targets_alter().
15
 *
16
 * @see FeedsNodeProcessor::getMappingTargets().
10
 * Implements hook_feeds_processor_targets().
17 11
 */
18
function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
12
function file_feeds_processor_targets($entity_type, $bundle_name) {
13
  $targets = array();
14

  
19 15
  foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
20 16
    $info = field_info_field($name);
21 17

  
22 18
    if (in_array($info['type'], array('file', 'image'))) {
23
      $targets[$name] = array(
24
        'name' => check_plain($instance['label']),
19
      $targets[$name . ':uri'] = array(
20
        'name' => t('@label: URI', array('@label' => $instance['label'])),
25 21
        'callback' => 'file_feeds_set_target',
26
        'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
22
        'description' => t('The URI of the @label field.', array('@label' => $instance['label'])),
23
        'real_target' => $name,
27 24
      );
25

  
26
      if ($info['type'] == 'image') {
27
        $targets[$name . ':alt'] = array(
28
          'name' => t('@label: Alt', array('@label' => $instance['label'])),
29
          'callback' => 'file_feeds_set_target',
30
          'description' => t('The alt tag of the @label field.', array('@label' => $instance['label'])),
31
          'real_target' => $name,
32
        );
33
        $targets[$name . ':title'] = array(
34
          'name' => t('@label: Title', array('@label' => $instance['label'])),
35
          'callback' => 'file_feeds_set_target',
36
          'description' => t('The title of the @label field.', array('@label' => $instance['label'])),
37
          'real_target' => $name,
38
        );
39
      }
40
      elseif ($info['type'] === 'file') {
41
        $targets[$name . ':description'] = array(
42
          'name' => t('@label: Description', array('@label' => $instance['label'])),
43
          'callback' => 'file_feeds_set_target',
44
          'description' => t('The description of the @label field.', array('@label' => $instance['label'])),
45
          'real_target' => $name,
46
        );
47
      }
28 48
    }
29 49
  }
50

  
51
  return $targets;
30 52
}
31 53

  
32 54
/**
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.
55
 * Callback for mapping file fields.
38 56
 */
39
function file_feeds_set_target($source, $entity, $target, $value) {
40
  if (empty($value)) {
41
    return;
42
  }
43
  module_load_include('inc', 'file');
57
function file_feeds_set_target(FeedsSource $source, $entity, $target, array $values) {
58
  // Add default of uri for backwards compatibility.
59
  list($field_name, $sub_field) = explode(':', $target . ':uri');
60
  $info = field_info_field($field_name);
44 61

  
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]);
62
  if ($sub_field == 'uri') {
63

  
64
    foreach ($values as $k => $v) {
65
      if (!($v instanceof FeedsEnclosure)) {
66
        if (is_string($v)) {
67
          $values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
68
        }
69
        else {
70
          // Set the value for FALSE rather than remove it to keep our deltas
71
          // correct.
72
          $values[$k] = FALSE;
73
        }
56 74
      }
57 75
    }
58
  }
59
  if (empty($value)) {
60
    return;
61
  }
62 76

  
63
  $entity_type = $source->importer->processor->entityType();
77
    $entity_type = $source->importer->processor->entityType();
78
    $bundle = $source->importer->processor->bundle();
64 79

  
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;
80
    $instance_info = field_info_instance($entity_type, $field_name, $bundle);
81

  
82
    // Determine file destination.
83
    // @todo This needs review and debugging.
84
    $data = array();
85
    if (!empty($entity->uid)) {
86
      $data[$entity_type] = $entity;
87
    }
88

  
89
    $destination = file_field_widget_uri($info, $instance_info, $data);
72 90
  }
73
  $destination = file_field_widget_uri($info, $instance_info, $data);
74 91

  
75 92
  // 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);
93
  $field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array());
94
  $delta = 0;
95
  foreach ($values as $v) {
96
    if ($info['cardinality'] == $delta) {
97
      break;
83 98
    }
84
    catch (Exception $e) {
85
      watchdog('feeds', check_plain($e->getMessage()));
99

  
100
    if (!isset($field[LANGUAGE_NONE][$delta])) {
101
      $field[LANGUAGE_NONE][$delta] = array();
86 102
    }
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) {
103

  
104
    switch ($sub_field) {
105
      case 'alt':
106
      case 'title':
107
      case 'description':
108
        $field[LANGUAGE_NONE][$delta][$sub_field] = $v;
109
        break;
110

  
111
      case 'uri':
112
        if ($v) {
113
          try {
114
            $v->setAllowedExtensions($instance_info['settings']['file_extensions']);
115
            $field[LANGUAGE_NONE][$delta] += (array) $v->getFile($destination);
116
            // @todo: Figure out how to properly populate this field.
117
            $field[LANGUAGE_NONE][$delta]['display'] = 1;
118
          }
119
          catch (Exception $e) {
120
            watchdog('feeds', check_plain($e->getMessage()));
121
          }
122
        }
91 123
        break;
92
      }
93
      $i++;
94 124
    }
125

  
126
    $delta++;
95 127
  }
96
  $entity->{$target} = $field;
128

  
129
  $entity->$field_name = $field;
97 130
}

Formats disponibles : Unified diff