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 @ fc2c1c7a

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * On behalf implementation of Feeds mapping API for file.module and
6
 * image.module.
7
 */
8
9
/**
10 41cc1b08 Assos Assos
 * Implements hook_feeds_processor_targets().
11 85ad3d82 Assos Assos
 */
12 41cc1b08 Assos Assos
function file_feeds_processor_targets($entity_type, $bundle_name) {
13
  $targets = array();
14
15 85ad3d82 Assos Assos
  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 41cc1b08 Assos Assos
      $targets[$name . ':uri'] = array(
20
        'name' => t('@label: URI', array('@label' => $instance['label'])),
21 85ad3d82 Assos Assos
        'callback' => 'file_feeds_set_target',
22 41cc1b08 Assos Assos
        'description' => t('The URI of the @label field.', array('@label' => $instance['label'])),
23
        'real_target' => $name,
24 85ad3d82 Assos Assos
      );
25 41cc1b08 Assos Assos
26 a192dc0b Assos Assos
      // 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 41cc1b08 Assos Assos
      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 85ad3d82 Assos Assos
    }
54
  }
55 41cc1b08 Assos Assos
56
  return $targets;
57 85ad3d82 Assos Assos
}
58
59
/**
60 41cc1b08 Assos Assos
 * Callback for mapping file fields.
61 85ad3d82 Assos Assos
 */
62 a192dc0b Assos Assos
function file_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
63
  $language = $mapping['language'];
64
65 41cc1b08 Assos Assos
  // Add default of uri for backwards compatibility.
66
  list($field_name, $sub_field) = explode(':', $target . ':uri');
67
  $info = field_info_field($field_name);
68 85ad3d82 Assos Assos
69 41cc1b08 Assos Assos
  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 85ad3d82 Assos Assos
      }
82
    }
83
84 a192dc0b Assos Assos
    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 41cc1b08 Assos Assos
    $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 85ad3d82 Assos Assos
  }
103
104
  // Populate entity.
105 a192dc0b Assos Assos
  $field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
106 41cc1b08 Assos Assos
  $delta = 0;
107
  foreach ($values as $v) {
108
    if ($info['cardinality'] == $delta) {
109
      break;
110 85ad3d82 Assos Assos
    }
111 41cc1b08 Assos Assos
112 a192dc0b Assos Assos
    if (!isset($field[$language][$delta])) {
113
      $field[$language][$delta] = array();
114 85ad3d82 Assos Assos
    }
115 41cc1b08 Assos Assos
116
    switch ($sub_field) {
117
      case 'alt':
118
      case 'title':
119
      case 'description':
120 a192dc0b Assos Assos
        $field[$language][$delta][$sub_field] = $v;
121 41cc1b08 Assos Assos
        break;
122
123
      case 'uri':
124
        if ($v) {
125
          try {
126
            $v->setAllowedExtensions($instance_info['settings']['file_extensions']);
127 a192dc0b Assos Assos
            $field[$language][$delta] += (array) $v->getFile($destination);
128 41cc1b08 Assos Assos
            // @todo: Figure out how to properly populate this field.
129 a192dc0b Assos Assos
            $field[$language][$delta]['display'] = 1;
130 41cc1b08 Assos Assos
          }
131
          catch (Exception $e) {
132
            watchdog('feeds', check_plain($e->getMessage()));
133
          }
134
        }
135 85ad3d82 Assos Assos
        break;
136
    }
137 41cc1b08 Assos Assos
138
    $delta++;
139 85ad3d82 Assos Assos
  }
140 41cc1b08 Assos Assos
141
  $entity->$field_name = $field;
142 85ad3d82 Assos Assos
}