Projet

Général

Profil

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

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

1
<?php
2

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

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

    
14
  $text_types = array(
15
    'text',
16
    'text_long',
17
    'text_with_summary',
18
  );
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'], $text_types)) {
23
      $targets[$name] = array(
24
        'name' => check_plain($instance['label']),
25
        'callback' => 'text_feeds_set_target',
26
        'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
27
      );
28
      if ($info['type'] == 'text_with_summary') {
29
        // Allow mapping to summary.
30
        $targets[$name . ':summary'] = array(
31
          'name' => t('@name: Summary', array('@name' => $instance['label'])),
32
          'callback' => 'text_feeds_set_target',
33
          'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
34
          'real_target' => $name,
35
        );
36
      }
37
    }
38

    
39
    if (!empty($instance['settings']['text_processing'])) {
40
      $targets[$name]['summary_callbacks'] = array('text_feeds_summary_callback');
41
      $targets[$name]['form_callbacks'] = array('text_feeds_form_callback');
42
    }
43
  }
44

    
45
  return $targets;
46
}
47

    
48
/**
49
 * Callback for mapping text fields.
50
 */
51
function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
52
  list($field_name, $column) = explode(':', $target . ':value');
53

    
54
  if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
55
    $format = $source->importer->processor->config['input_format'];
56
    // Add in default values.
57
    $mapping += array(
58
      'format' => $format,
59
    );
60
  }
61

    
62
  $field = isset($entity->$field_name) ? $entity->$field_name : array('und' => array());
63

    
64
  // Iterate over all values.
65
  $delta = 0;
66
  foreach ($values as $value) {
67

    
68
    if (is_object($value) && $value instanceof FeedsElement) {
69
      $value = $value->getValue();
70
    }
71

    
72
    if (is_scalar($value) && strlen($value)) {
73

    
74
      $field['und'][$delta][$column] = (string) $value;
75

    
76
      if (isset($mapping['format'])) {
77
        $field['und'][$delta]['format'] = $mapping['format'];
78
      }
79
    }
80

    
81
    $delta++;
82
  }
83

    
84
  $entity->$field_name = $field;
85
}
86

    
87
/**
88
 * Summary callback for text field targets.
89
 *
90
 * Displays which text format will be used for the text field target.
91
 *
92
 * @see text_feeds_processor_targets()
93
 * @see text_feeds_form_callback()
94
 */
95
function text_feeds_summary_callback(array $mapping, $target, array $form, array $form_state) {
96
  global $user;
97
  $formats = filter_formats($user);
98

    
99
  // Processor-wide input format setting.
100
  $importer = feeds_importer($form['#importer']);
101
  $default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
102
  $mapping += array(
103
    'format' => $default_format,
104
  );
105

    
106
  return t('Text format: %format', array('%format' => $formats[$mapping['format']]->name));
107
}
108

    
109
/**
110
 * Form callback for text field targets.
111
 *
112
 * Allows to select a text format for the text field target.
113
 *
114
 * @see text_feeds_processor_targets()
115
 * @see text_feeds_summary_callback()
116
 */
117
function text_feeds_form_callback(array $mapping, $target, array $form, array $form_state) {
118
  global $user;
119
  $formats_options = array();
120
  $formats = filter_formats($user);
121
  foreach ($formats as $id => $format) {
122
    $formats_options[$id] = $format->name;
123
  }
124

    
125
  // Processor-wide text format setting.
126
  $importer = feeds_importer($form['#importer']);
127
  $default_format = !empty($importer->processor->config['input_format']) ? $importer->processor->config['input_format'] : filter_fallback_format();
128
  $mapping += array(
129
    'format' => $default_format,
130
  );
131

    
132
  return array(
133
    'format' => array(
134
      '#type' => 'select',
135
      '#title' => t('Text format'),
136
      '#options' => $formats_options,
137
      '#default_value' => $mapping['format'],
138
    ),
139
  );
140
}