1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Make field required. If not present, the feed item will be removed.
|
6
|
*/
|
7
|
|
8
|
$plugin = array(
|
9
|
'form' => 'feeds_tamper_required_form',
|
10
|
'callback' => 'feeds_tamper_required_callback',
|
11
|
'name' => 'Required field',
|
12
|
'multi' => 'direct',
|
13
|
'category' => 'Filter',
|
14
|
);
|
15
|
|
16
|
function feeds_tamper_required_form($importer, $element_key, $settings) {
|
17
|
$form = array();
|
18
|
|
19
|
$form['help'] = array(
|
20
|
'#markup' => t('Make this field required. If it is empty, the feed item will not be saved.'),
|
21
|
);
|
22
|
|
23
|
$form['invert'] = array(
|
24
|
'#type' => 'checkbox',
|
25
|
'#title' => t('Invert filter'),
|
26
|
'#default_value' => !empty($settings['invert']),
|
27
|
'#description' => t('Inverting the filter will save items only if the field is empty.'),
|
28
|
);
|
29
|
|
30
|
$form['log'] = array(
|
31
|
'#type' => 'checkbox',
|
32
|
'#default_value' => !empty($settings['log']),
|
33
|
'#title' => t('Log'),
|
34
|
'#description' => t('Log to the Feed log and print a message when a required feed item is filtered.'),
|
35
|
);
|
36
|
|
37
|
return $form;
|
38
|
}
|
39
|
|
40
|
function feeds_tamper_required_callback($result, $item_key, $element_key, &$field, $settings, $source) {
|
41
|
if (!empty($settings['invert'])) {
|
42
|
if (!empty($field)) {
|
43
|
unset($result->items[$item_key]);
|
44
|
}
|
45
|
|
46
|
return;
|
47
|
}
|
48
|
|
49
|
if (!empty($field)) {
|
50
|
return;
|
51
|
}
|
52
|
|
53
|
unset($result->items[$item_key]);
|
54
|
|
55
|
if (!empty($settings['log'])) {
|
56
|
_feeds_tamper_required_log($source, $item_key, $element_key);
|
57
|
}
|
58
|
}
|
59
|
|
60
|
function _feeds_tamper_required_log($source, $item_key, $element_key) {
|
61
|
$args = array('@key' => $element_key, '@num' => $item_key);
|
62
|
|
63
|
$source->log('feeds_tamper:required', 'Feed item number @num skipped because @key was empty.', $args);
|
64
|
drupal_set_message(t('Feed item number @num skipped because @key was empty.', $args));
|
65
|
}
|