Projet

Général

Profil

Paste
Télécharger (4,28 ko) Statistiques
| Branche: | Révision:

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

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
      // Keep the old target name for backwards compatibility, but hide it from
27
      // the UI.
28
      $targets[$name] = $targets[$name . ':uri'];
29
      $targets[$name]['deprecated'] = TRUE;
30

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

    
56
  return $targets;
57
}
58

    
59
/**
60
 * Callback for mapping file fields.
61
 */
62
function file_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
63
  $language = $mapping['language'];
64

    
65
  // Add default of uri for backwards compatibility.
66
  list($field_name, $sub_field) = explode(':', $target . ':uri');
67
  $info = field_info_field($field_name);
68

    
69
  if ($sub_field == 'uri') {
70

    
71
    foreach ($values as $k => $v) {
72
      if (!($v instanceof FeedsEnclosure)) {
73
        if (is_string($v)) {
74
          $values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
75
        }
76
        else {
77
          // Set the value for FALSE rather than remove it to keep our deltas
78
          // correct.
79
          $values[$k] = FALSE;
80
        }
81
      }
82
    }
83

    
84
    if ($entity instanceof Entity) {
85
      $entity_type = $entity->entityType();
86
      $bundle = $entity->bundle();
87
    }
88
    else {
89
      $entity_type = $source->importer->processor->entityType();
90
      $bundle = $source->importer->processor->bundle();
91
    }
92
    $instance_info = field_info_instance($entity_type, $field_name, $bundle);
93

    
94
    // Determine file destination.
95
    // @todo This needs review and debugging.
96
    $data = array();
97
    if (!empty($entity->uid)) {
98
      $data[$entity_type] = $entity;
99
    }
100

    
101
    $destination = file_field_widget_uri($info, $instance_info, $data);
102
  }
103

    
104
  // Populate entity.
105
  $field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
106
  $delta = 0;
107
  foreach ($values as $v) {
108
    if ($info['cardinality'] == $delta) {
109
      break;
110
    }
111

    
112
    if (!isset($field[$language][$delta])) {
113
      $field[$language][$delta] = array();
114
    }
115

    
116
    switch ($sub_field) {
117
      case 'alt':
118
      case 'title':
119
      case 'description':
120
        $field[$language][$delta][$sub_field] = $v;
121
        break;
122

    
123
      case 'uri':
124
        if ($v) {
125
          try {
126
            $v->setAllowedExtensions($instance_info['settings']['file_extensions']);
127
            $field[$language][$delta] += (array) $v->getFile($destination);
128
            // @todo: Figure out how to properly populate this field.
129
            $field[$language][$delta]['display'] = 1;
130
          }
131
          catch (Exception $e) {
132
            watchdog('feeds', check_plain($e->getMessage()));
133
          }
134
        }
135
        break;
136
    }
137

    
138
    $delta++;
139
  }
140

    
141
  $entity->$field_name = $field;
142
}