Projet

Général

Profil

Paste
Télécharger (3,93 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / mappers / file.inc @ 41cc1b08

1
<?php
2

    
3
/**
4
 * @file
5
 * On behalf implementation of Feeds mapping API for file.module and
6
 * image.module.
7
 */
8

    
9
/**
10
 * Implements hook_feeds_processor_targets().
11
 */
12
function file_feeds_processor_targets($entity_type, $bundle_name) {
13
  $targets = array();
14

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

    
18
    if (in_array($info['type'], array('file', 'image'))) {
19
      $targets[$name . ':uri'] = array(
20
        'name' => t('@label: URI', array('@label' => $instance['label'])),
21
        'callback' => 'file_feeds_set_target',
22
        'description' => t('The URI of the @label field.', array('@label' => $instance['label'])),
23
        'real_target' => $name,
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
      }
48
    }
49
  }
50

    
51
  return $targets;
52
}
53

    
54
/**
55
 * Callback for mapping file fields.
56
 */
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);
61

    
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
        }
74
      }
75
    }
76

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

    
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);
90
  }
91

    
92
  // Populate entity.
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;
98
    }
99

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

    
126
    $delta++;
127
  }
128

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