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 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * On behalf implementation of Feeds mapping API for text.module.
6
 */
7
8
/**
9 41cc1b08 Assos Assos
 * Implements hook_feeds_processor_targets().
10 85ad3d82 Assos Assos
 */
11 41cc1b08 Assos Assos
function text_feeds_processor_targets($entity_type, $bundle_name) {
12
  $targets = array();
13
14 85ad3d82 Assos Assos
  $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 41cc1b08 Assos Assos
      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 85ad3d82 Assos Assos
    }
43
  }
44 41cc1b08 Assos Assos
45
  return $targets;
46 85ad3d82 Assos Assos
}
47
48
/**
49
 * Callback for mapping text fields.
50
 */
51 41cc1b08 Assos Assos
function text_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
52
  list($field_name, $column) = explode(':', $target . ':value');
53 85ad3d82 Assos Assos
54 41cc1b08 Assos Assos
  if ($column === 'value' && isset($source->importer->processor->config['input_format'])) {
55 85ad3d82 Assos Assos
    $format = $source->importer->processor->config['input_format'];
56 41cc1b08 Assos Assos
    // Add in default values.
57
    $mapping += array(
58
      'format' => $format,
59
    );
60 85ad3d82 Assos Assos
  }
61
62 41cc1b08 Assos Assos
  $field = isset($entity->$field_name) ? $entity->$field_name : array('und' => array());
63 85ad3d82 Assos Assos
64
  // Iterate over all values.
65 41cc1b08 Assos Assos
  $delta = 0;
66
  foreach ($values as $value) {
67 85ad3d82 Assos Assos
68 41cc1b08 Assos Assos
    if (is_object($value) && $value instanceof FeedsElement) {
69
      $value = $value->getValue();
70
    }
71 85ad3d82 Assos Assos
72 41cc1b08 Assos Assos
    if (is_scalar($value) && strlen($value)) {
73 85ad3d82 Assos Assos
74 41cc1b08 Assos Assos
      $field['und'][$delta][$column] = (string) $value;
75 85ad3d82 Assos Assos
76 41cc1b08 Assos Assos
      if (isset($mapping['format'])) {
77
        $field['und'][$delta]['format'] = $mapping['format'];
78
      }
79 85ad3d82 Assos Assos
    }
80
81 41cc1b08 Assos Assos
    $delta++;
82
  }
83 85ad3d82 Assos Assos
84 41cc1b08 Assos Assos
  $entity->$field_name = $field;
85
}
86 85ad3d82 Assos Assos
87 41cc1b08 Assos Assos
/**
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 85ad3d82 Assos Assos
  }
124
125 41cc1b08 Assos Assos
  // 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 85ad3d82 Assos Assos
}