1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* On behalf implementation of Feeds mapping API for date
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_feeds_processor_targets_alter().
|
10
|
*
|
11
|
* @see FeedsNodeProcessor::getMappingTargets().
|
12
|
*
|
13
|
* @todo Only provides "end date" target if field allows it.
|
14
|
*/
|
15
|
function date_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
|
16
|
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
|
17
|
$info = field_info_field($name);
|
18
|
if (in_array($info['type'], array('date', 'datestamp', 'datetime'))) {
|
19
|
$targets[$name . ':start'] = array(
|
20
|
'name' => t('@name: Start', array('@name' => $instance['label'])),
|
21
|
'callback' => 'date_feeds_set_target',
|
22
|
'description' => t('The start date for the @name field. Also use if mapping both start and end.', array('@name' => $instance['label'])),
|
23
|
'real_target' => $name,
|
24
|
);
|
25
|
$targets[$name . ':end'] = array(
|
26
|
'name' => t('@name: End', array('@name' => $instance['label'])),
|
27
|
'callback' => 'date_feeds_set_target',
|
28
|
'description' => t('The end date for the @name field.', array('@name' => $instance['label'])),
|
29
|
'real_target' => $name,
|
30
|
);
|
31
|
}
|
32
|
}
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Implements hook_feeds_set_target().
|
37
|
*
|
38
|
* @param $node
|
39
|
* The target node.
|
40
|
* @param $field_name
|
41
|
* The name of field on the target node to map to.
|
42
|
* @param $feed_element
|
43
|
* The value to be mapped. Should be either a (flexible) date string
|
44
|
* or a FeedsDateTimeElement object.
|
45
|
*
|
46
|
*/
|
47
|
function date_feeds_set_target($source, $entity, $target, $feed_element) {
|
48
|
list($field_name, $sub_field) = explode(':', $target, 2);
|
49
|
if (!($feed_element instanceof FeedsDateTimeElement)) {
|
50
|
if (!is_array($feed_element)) {
|
51
|
$feed_element = array($feed_element);
|
52
|
}
|
53
|
$delta = 0;
|
54
|
foreach ($feed_element as $f) {
|
55
|
if (empty($f) || !is_numeric($f) && !date_create($f)) {
|
56
|
$array_element = new FeedsDateTimeElement(NULL, NULL);
|
57
|
}
|
58
|
elseif ($sub_field == 'end') {
|
59
|
$array_element = new FeedsDateTimeElement(NULL, $f);
|
60
|
}
|
61
|
else {
|
62
|
$array_element = new FeedsDateTimeElement($f, NULL);
|
63
|
}
|
64
|
$array_element->buildDateField($entity, $field_name, $delta);
|
65
|
$delta++;
|
66
|
}
|
67
|
}
|
68
|
}
|