-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedsMultiFeedNodeProcessor.inc
More file actions
132 lines (119 loc) · 4.52 KB
/
FeedsMultiFeedNodeProcessor.inc
File metadata and controls
132 lines (119 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
// $Id$
/**
* @file
* Class definition of FeedsMultiFeedNodeProcessor.
*/
/**
* Creates *feed* nodes from feed items. The difference to FeedsFeedNodeProcessor is
* that this plugin allows creation of feed nodes of different type.
*/
class FeedsMultiFeedNodeProcessor extends FeedsNodeProcessor {
/**
* Implementation of FeedsProcessor::clear().
*/
public function clear(FeedsBatch $batch, FeedsSource $source) {
// Do not support deleting imported items as we would have to delete all
// items of the content type we imported which may contain nodes that a
// user created by hand.
throw new Exception(t('This configuration does not support deleting imported items.'));
}
/**
* Execute mapping on an item.
*/
protected function map($source_item, $target_node) {
// Prepare node object.
static $included;
if (!$included) {
module_load_include('inc', 'node', 'node.pages');
$included = TRUE;
}
$target_node->feeds = array();
// Suppress auto import, we may be creating many feeds
$target_node->feeds['suppress_import'] = TRUE;
node_object_prepare($target_node); // double object prepare...
/*
Assign an aggregated node always to current user.
@todo This won't work in all cases as the assumption here is that
import is happening one off when user is logged in. Assumption breaks if
feed node processor is being used for aggregation on cron time and a
specific user should still be the owner of the imported feed nodes.
*/
global $user;
$target_node->uid = $user->uid;
// Have parent class do the iterating.
return parent::map($source_item, $target_node);
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
$defaults = parent::configDefaults();
$defaults['content_type'] = ''; // reset content type
return $defaults;
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
$form = parent::configForm($form_state);
$feeds = feeds_importer_load_all();
$types = array();
foreach ($feeds as $feed) {
if ($feed->id != $this->id && !empty($feed->config['content_type'])) {
$types[$feed->config['content_type']] = node_get_types('name', $feed->config['content_type']);
}
}
if (empty($types)) {
$types[''] = t('No feed node content type available');
}
else {
$types = array(
'' => t('Select'),
) + $types;
}
// Overwrite content type
$form['content_type'] = array(
'#type' => 'select',
'#title' => t('Content type'),
'#description' => t('Choose <strong>default</strong> node type to create from this feed. This can be overwritten if the mapping target "node type" is assigend. Only node types with attached importer configurations are listed here. <strong>Note:</strong> Users with "import !feed_id feeds" permissions will be able to <strong>import</strong> nodes of the content type selected here regardless of the node level permissions. However, users with "clear !feed_id permissions" need to have sufficient node level permissions to delete the imported nodes.', array('!feed_id' => $this->id)),
'#options' => $types,
'#default_value' => $this->config['content_type'],
);
return $form;
}
/**
* Override setTargetElement to operate on a target item that is a node.
*/
public function setTargetElement($target_node, $target_element, $value) {
parent::setTargetElement($target_node, $target_element, $value);
if ($target_element == 'url') {
// Get the class of the feed node importer's fetcher and set the source
// property. See feeds_nodeapi() how $node->feeds gets stored.
$class = get_class($this->feedNodeImporter()->fetcher);
$target_node->feeds[$class]['source'] = $value;
}
elseif ($target_element == 'type') {
if (feeds_get_importer_id($value)) {
// only accept node types with attached import configuration
$target_node->type = $value;
}
else {
// set default value
$target_node->type = $this->config['content_type'];
}
node_object_prepare($target_node);
}
}
/**
* Helper for retrieving the importer object for the feed nodes to produce.
*/
protected function feedNodeImporter() {
if ($id = feeds_get_importer_id($this->config['content_type'])) {
return feeds_importer($id);
}
else {
throw new Exception(t('Content type to be created is not a valid Feed content type.'));
}
}
}